home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / gcc263-utildoc.lha / gnu / man / man1 / flexdoc.1 < prev    next >
Encoding:
Text File  |  1994-12-19  |  107.9 KB  |  2,773 lines

  1.  
  2.  
  3.  
  4. FLEXDOC(1)                                             FLEXDOC(1)
  5.  
  6.  
  7. NNAAMMEE
  8.        flexdoc  -  documentation  for flex, fast lexical analyzer
  9.        generator
  10.  
  11. SSYYNNOOPPSSIISS
  12.        fflleexx [[--bbccddffhhiillnnppssttvvwwBBFFIILLTTVV7788++ --CC[[aaeeffFFmmrr]] --PPpprreeffiixx --SSsskkeellee--
  13.        ttoonn]] _[_f_i_l_e_n_a_m_e _._._._]
  14.  
  15. DDEESSCCRRIIPPTTIIOONN
  16.        _f_l_e_x  is  a  tool  for generating _s_c_a_n_n_e_r_s_: programs which
  17.        recognized lexical patterns in text.  _f_l_e_x reads the given
  18.        input  files,  or  its standard input if no file names are
  19.        given, for a description of a scanner  to  generate.   The
  20.        description is in the form of pairs of regular expressions
  21.        and C code, called _r_u_l_e_s_. _f_l_e_x generates  as  output  a  C
  22.        source  file,  lleexx..yyyy..cc,,  which defines a routine yyyylleexx(())..
  23.        This file is compiled and linked with the --llffll library  to
  24.        produce  an  executable.   When  the executable is run, it
  25.        analyzes its input for occurrences of the regular  expres-
  26.        sions.  Whenever it finds one, it executes the correspond-
  27.        ing C code.
  28.  
  29. SSOOMMEE SSIIMMPPLLEE EEXXAAMMPPLLEESS
  30.        First some simple examples to get the flavor  of  how  one
  31.        uses  _f_l_e_x_.   The following _f_l_e_x input specifies a scanner
  32.        which whenever it encounters the  string  "username"  will
  33.        replace it with the user's login name:
  34.  
  35.            %%
  36.            username    printf( "%s", getlogin() );
  37.  
  38.        By  default,  any  text  not  matched by a _f_l_e_x scanner is
  39.        copied to the output, so the net effect of this scanner is
  40.        to  copy its input file to its output with each occurrence
  41.        of "username" expanded.  In this input, there is just  one
  42.        rule.   "username"  is the _p_a_t_t_e_r_n and the "printf" is the
  43.        _a_c_t_i_o_n_.  The "%%" marks the beginning of the rules.
  44.  
  45.        Here's another simple example:
  46.  
  47.                    int num_lines = 0, num_chars = 0;
  48.  
  49.            %%
  50.            \n      ++num_lines; ++num_chars;
  51.            .       ++num_chars;
  52.  
  53.            %%
  54.            main()
  55.                    {
  56.                    yylex();
  57.                    printf( "# of lines = %d, # of chars = %d\n",
  58.                            num_lines, num_chars );
  59.                    }
  60.  
  61.  
  62.  
  63.  
  64. Version 2.4               November 1993                         1
  65.  
  66.  
  67.  
  68.  
  69.  
  70. FLEXDOC(1)                                             FLEXDOC(1)
  71.  
  72.  
  73.        This scanner counts the number of characters and the  num-
  74.        ber  of  lines  in  its input (it produces no output other
  75.        than the final report on  the  counts).   The  first  line
  76.        declares  two  globals, "num_lines" and "num_chars", which
  77.        are accessible both inside yyyylleexx(()) and in the mmaaiinn(())  rou-
  78.        tine declared after the second "%%".  There are two rules,
  79.        one which matches a newline ("\n") and increments both the
  80.        line  count and the character count, and one which matches
  81.        any character other than a newline (indicated by  the  "."
  82.        regular expression).
  83.  
  84.        A somewhat more complicated example:
  85.  
  86.            /* scanner for a toy Pascal-like language */
  87.  
  88.            %{
  89.            /* need this for the call to atof() below */
  90.            #include <math.h>
  91.            %}
  92.  
  93.            DIGIT    [0-9]
  94.            ID       [a-z][a-z0-9]*
  95.  
  96.            %%
  97.  
  98.            {DIGIT}+    {
  99.                        printf( "An integer: %s (%d)\n", yytext,
  100.                                atoi( yytext ) );
  101.                        }
  102.  
  103.            {DIGIT}+"."{DIGIT}*        {
  104.                        printf( "A float: %s (%g)\n", yytext,
  105.                                atof( yytext ) );
  106.                        }
  107.  
  108.            if|then|begin|end|procedure|function        {
  109.                        printf( "A keyword: %s\n", yytext );
  110.                        }
  111.  
  112.            {ID}        printf( "An identifier: %s\n", yytext );
  113.  
  114.            "+"|"-"|"*"|"/"   printf( "An operator: %s\n", yytext );
  115.  
  116.            "{"[^}\n]*"}"     /* eat up one-line comments */
  117.  
  118.            [ \t\n]+          /* eat up whitespace */
  119.  
  120.            .           printf( "Unrecognized character: %s\n", yytext );
  121.  
  122.            %%
  123.  
  124.            main( argc, argv )
  125.            int argc;
  126.            char **argv;
  127.  
  128.  
  129.  
  130. Version 2.4               November 1993                         2
  131.  
  132.  
  133.  
  134.  
  135.  
  136. FLEXDOC(1)                                             FLEXDOC(1)
  137.  
  138.  
  139.                {
  140.                ++argv, --argc;  /* skip over program name */
  141.                if ( argc > 0 )
  142.                        yyin = fopen( argv[0], "r" );
  143.                else
  144.                        yyin = stdin;
  145.  
  146.                yylex();
  147.                }
  148.  
  149.        This  is the beginnings of a simple scanner for a language
  150.        like Pascal.  It identifies different types of _t_o_k_e_n_s  and
  151.        reports on what it has seen.
  152.  
  153.        The  details of this example will be explained in the fol-
  154.        lowing sections.
  155.  
  156. FFOORRMMAATT OOFF TTHHEE IINNPPUUTT FFIILLEE
  157.        The _f_l_e_x input file consists of three sections,  separated
  158.        by a line with just %%%% in it:
  159.  
  160.            definitions
  161.            %%
  162.            rules
  163.            %%
  164.            user code
  165.  
  166.        The  _d_e_f_i_n_i_t_i_o_n_s  section  contains declarations of simple
  167.        _n_a_m_e definitions to simplify  the  scanner  specification,
  168.        and  declarations of _s_t_a_r_t _c_o_n_d_i_t_i_o_n_s_, which are explained
  169.        in a later section.
  170.  
  171.        Name definitions have the form:
  172.  
  173.            name definition
  174.  
  175.        The "name" is a word beginning with a letter or an  under-
  176.        score ('_') followed by zero or more letters, digits, '_',
  177.        or '-' (dash).  The definition is taken to  begin  at  the
  178.        first  non-white-space  character  following  the name and
  179.        continuing to the end of the  line.   The  definition  can
  180.        subsequently  be  referred  to  using "{name}", which will
  181.        expand to "(definition)".  For example,
  182.  
  183.            DIGIT    [0-9]
  184.            ID       [a-z][a-z0-9]*
  185.  
  186.        defines "DIGIT" to be a regular expression which matches a
  187.        single  digit,  and  "ID" to be a regular expression which
  188.        matches a  letter  followed  by  zero-or-more  letters-or-
  189.        digits.  A subsequent reference to
  190.  
  191.            {DIGIT}+"."{DIGIT}*
  192.  
  193.  
  194.  
  195.  
  196. Version 2.4               November 1993                         3
  197.  
  198.  
  199.  
  200.  
  201.  
  202. FLEXDOC(1)                                             FLEXDOC(1)
  203.  
  204.  
  205.        is identical to
  206.  
  207.            ([0-9])+"."([0-9])*
  208.  
  209.        and  matches one-or-more digits followed by a '.' followed
  210.        by zero-or-more digits.
  211.  
  212.        The _r_u_l_e_s section of the _f_l_e_x input contains a  series  of
  213.        rules of the form:
  214.  
  215.            pattern   action
  216.  
  217.        where  the  pattern must be unindented and the action must
  218.        begin on the same line.
  219.  
  220.        See below  for  a  further  description  of  patterns  and
  221.        actions.
  222.  
  223.        Finally,  the  user  code  section  is  simply  copied  to
  224.        lleexx..yyyy..cc verbatim.  It  is  used  for  companion  routines
  225.        which  call or are called by the scanner.  The presence of
  226.        this section is optional; if it is missing, the second  %%%%
  227.        in the input file may be skipped, too.
  228.  
  229.        In  the  definitions and rules sections, any _i_n_d_e_n_t_e_d text
  230.        or text enclosed in %%{{ and %%}} is copied  verbatim  to  the
  231.        output  (with  the  %{}'s removed).  The %{}'s must appear
  232.        unindented on lines by themselves.
  233.  
  234.        In the rules section, any indented or %{}  text  appearing
  235.        before  the  first  rule  may be used to declare variables
  236.        which are local to the scanning  routine  and  (after  the
  237.        declarations)  code  which  is to be executed whenever the
  238.        scanning routine is entered.  Other indented or  %{}  text
  239.        in the rule section is still copied to the output, but its
  240.        meaning is not well-defined and it may well cause compile-
  241.        time errors (this feature is present for _P_O_S_I_X compliance;
  242.        see below for other such features).
  243.  
  244.        In the definitions section (but not in the rules section),
  245.        an  unindented  comment (i.e., a line beginning with "/*")
  246.        is also copied verbatim to the output up to the next "*/".
  247.  
  248. PPAATTTTEERRNNSS
  249.        The  patterns  in  the input are written using an extended
  250.        set of regular expressions.  These are:
  251.  
  252.            x          match the character 'x'
  253.            .          any character except newline
  254.            [xyz]      a "character class"; in this case, the pattern
  255.                         matches either an 'x', a 'y', or a 'z'
  256.            [abj-oZ]   a "character class" with a range in it; matches
  257.                         an 'a', a 'b', any letter from 'j' through 'o',
  258.                         or a 'Z'
  259.  
  260.  
  261.  
  262. Version 2.4               November 1993                         4
  263.  
  264.  
  265.  
  266.  
  267.  
  268. FLEXDOC(1)                                             FLEXDOC(1)
  269.  
  270.  
  271.            [^A-Z]     a "negated character class", i.e., any character
  272.                         but those in the class.  In this case, any
  273.                         character EXCEPT an uppercase letter.
  274.            [^A-Z\n]   any character EXCEPT an uppercase letter or
  275.                         a newline
  276.            r*         zero or more r's, where r is any regular expression
  277.            r+         one or more r's
  278.            r?         zero or one r's (that is, "an optional r")
  279.            r{2,5}     anywhere from two to five r's
  280.            r{2,}      two or more r's
  281.            r{4}       exactly 4 r's
  282.            {name}     the expansion of the "name" definition
  283.                       (see above)
  284.            "[xyz]\"foo"
  285.                       the literal string: [xyz]"foo
  286.            \X         if X is an 'a', 'b', 'f', 'n', 'r', 't', or 'v',
  287.                         then the ANSI-C interpretation of \x.
  288.                         Otherwise, a literal 'X' (used to escape
  289.                         operators such as '*')
  290.            \123       the character with octal value 123
  291.            \x2a       the character with hexadecimal value 2a
  292.            (r)        match an r; parentheses are used to override
  293.                         precedence (see below)
  294.  
  295.  
  296.            rs         the regular expression r followed by the
  297.                         regular expression s; called "concatenation"
  298.  
  299.  
  300.            r|s        either an r or an s
  301.  
  302.  
  303.            r/s        an r but only if it is followed by an s.  The
  304.                         s is not part of the matched text.  This type
  305.                         of pattern is called as "trailing context".
  306.            ^r         an r, but only at the beginning of a line
  307.            r$         an r, but only at the end of a line.  Equivalent
  308.                         to "r/\n".
  309.  
  310.  
  311.            <s>r       an r, but only in start condition s (see
  312.                       below for discussion of start conditions)
  313.            <s1,s2,s3>r
  314.                       same, but in any of start conditions s1,
  315.                       s2, or s3
  316.            <*>r       an r in any start condition, even an exclusive one.
  317.  
  318.  
  319.            <<EOF>>    an end-of-file
  320.            <s1,s2><<EOF>>
  321.                       an end-of-file when in start condition s1 or s2
  322.  
  323.        Note that inside of a character class, all regular expres-
  324.        sion  operators  lose  their special meaning except escape
  325.  
  326.  
  327.  
  328. Version 2.4               November 1993                         5
  329.  
  330.  
  331.  
  332.  
  333.  
  334. FLEXDOC(1)                                             FLEXDOC(1)
  335.  
  336.  
  337.        ('\') and the character class operators, '-', ']', and, at
  338.        the beginning of the class, '^'.
  339.  
  340.        The regular expressions listed above are grouped according
  341.        to precedence, from highest precedence at the top to  low-
  342.        est  at  the  bottom.   Those  grouped together have equal
  343.        precedence.  For example,
  344.  
  345.            foo|bar*
  346.  
  347.        is the same as
  348.  
  349.            (foo)|(ba(r*))
  350.  
  351.        since the '*' operator has higher precedence than concate-
  352.        nation,  and  concatenation higher than alternation ('|').
  353.        This pattern therefore matches _e_i_t_h_e_r the string "foo"  _o_r
  354.        the  string  "ba"  followed by zero-or-more r's.  To match
  355.        "foo" or zero-or-more "bar"'s, use:
  356.  
  357.            foo|(bar)*
  358.  
  359.        and to match zero-or-more "foo"'s-or-"bar"'s:
  360.  
  361.            (foo|bar)*
  362.  
  363.  
  364.        Some notes on patterns:
  365.  
  366.        -      A negated character class such as the example "[^A-
  367.               Z]"  above  _w_i_l_l _m_a_t_c_h _a _n_e_w_l_i_n_e unless "\n" (or an
  368.               equivalent escape sequence) is one of  the  charac-
  369.               ters  explicitly  present  in the negated character
  370.               class (e.g., "[^A-Z\n]").  This is unlike how  many
  371.               other  regular expression tools treat negated char-
  372.               acter classes, but unfortunately the  inconsistency
  373.               is   historically  entrenched.   Matching  newlines
  374.               means that a  pattern  like  [^"]*  can  match  the
  375.               entire  input  unless  there's another quote in the
  376.               input.
  377.  
  378.        -      A rule can have at most one  instance  of  trailing
  379.               context  (the  '/'  operator  or the '$' operator).
  380.               The start condition, '^',  and  "<<EOF>>"  patterns
  381.               can  only occur at the beginning of a pattern, and,
  382.               as well as with '/'  and  '$',  cannot  be  grouped
  383.               inside  parentheses.  A '^' which does not occur at
  384.               the beginning of a rule or a  '$'  which  does  not
  385.               occur  at the end of a rule loses its special prop-
  386.               erties and is treated as a normal character.
  387.  
  388.               The following are illegal:
  389.  
  390.                   foo/bar$
  391.  
  392.  
  393.  
  394. Version 2.4               November 1993                         6
  395.  
  396.  
  397.  
  398.  
  399.  
  400. FLEXDOC(1)                                             FLEXDOC(1)
  401.  
  402.  
  403.                   <sc1>foo<sc2>bar
  404.  
  405.               Note that  the  first  of  these,  can  be  written
  406.               "foo/bar\n".
  407.  
  408.               The  following  will  result  in  '$'  or '^' being
  409.               treated as a normal character:
  410.  
  411.                   foo|(bar$)
  412.                   foo|^bar
  413.  
  414.               If what's wanted is a "foo" or a bar-followed-by-a-
  415.               newline,  the  following could be used (the special
  416.               '|' action is explained below):
  417.  
  418.                   foo      |
  419.                   bar$     /* action goes here */
  420.  
  421.               A similar trick will work for matching a foo  or  a
  422.               bar-at-the-beginning-of-a-line.
  423.  
  424. HHOOWW TTHHEE IINNPPUUTT IISS MMAATTCCHHEEDD
  425.        When  the  generated scanner is run, it analyzes its input
  426.        looking for strings which match any of its  patterns.   If
  427.        it  finds  more  than one match, it takes the one matching
  428.        the most text (for trailing context rules,  this  includes
  429.        the  length of the trailing part, even though it will then
  430.        be returned to the  input).   If  it  finds  two  or  more
  431.        matches  of  the same length, the rule listed first in the
  432.        _f_l_e_x input file is chosen.
  433.  
  434.        Once the match is determined, the  text  corresponding  to
  435.        the  match  (called  the  _t_o_k_e_n_)  is made available in the
  436.        global character pointer yyyytteexxtt,, and  its  length  in  the
  437.        global  integer  yyyylleenngg..   The _a_c_t_i_o_n corresponding to the
  438.        matched pattern is then executed (a more detailed descrip-
  439.        tion  of actions follows), and then the remaining input is
  440.        scanned for another match.
  441.  
  442.        If no match is found, then the _d_e_f_a_u_l_t _r_u_l_e  is  executed:
  443.        the  next character in the input is considered matched and
  444.        copied to the standard output.  Thus, the  simplest  legal
  445.        _f_l_e_x input is:
  446.  
  447.            %%
  448.  
  449.        which  generates  a  scanner  that simply copies its input
  450.        (one character at a time) to its output.
  451.  
  452.        Note that yyyytteexxtt can be defined  in  two  different  ways:
  453.        either  as  a  character  _p_o_i_n_t_e_r or as a character _a_r_r_a_y_.
  454.        You can control which definition _f_l_e_x  uses  by  including
  455.        one  of  the  special directives %%ppooiinntteerr or %%aarrrraayy in the
  456.        first (definitions)  section  of  your  flex  input.   The
  457.  
  458.  
  459.  
  460. Version 2.4               November 1993                         7
  461.  
  462.  
  463.  
  464.  
  465.  
  466. FLEXDOC(1)                                             FLEXDOC(1)
  467.  
  468.  
  469.        default is %%ppooiinntteerr,, unless you use the --ll lex compatibil-
  470.        ity option, in which case yyyytteexxtt will be  an  array.   The
  471.        advantage  of using %%ppooiinntteerr is substantially faster scan-
  472.        ning and no  buffer  overflow  when  matching  very  large
  473.        tokens (unless you run out of dynamic memory).  The disad-
  474.        vantage is that you are restricted in how your actions can
  475.        modify  yyyytteexxtt  (see  the  next section), and calls to the
  476.        iinnppuutt(()) and uunnppuutt(()) functions destroy the present contents
  477.        of  yyyytteexxtt,,  which  can be a considerable porting headache
  478.        when moving between different _l_e_x versions.
  479.  
  480.        The advantage of %%aarrrraayy is that you can then modify yyyytteexxtt
  481.        to  your heart's content, and calls to iinnppuutt(()) and uunnppuutt(())
  482.        do not destroy yyyytteexxtt (see below).  Furthermore,  existing
  483.        _l_e_x programs sometimes access yyyytteexxtt externally using dec-
  484.        larations of the form:
  485.            extern char yytext[];
  486.        This definition is erroneous when used with %%ppooiinntteerr,,  but
  487.        correct for %%aarrrraayy..
  488.  
  489.        %%aarrrraayy defines yyyytteexxtt to be an array of YYYYLLMMAAXX characters,
  490.        which defaults to a fairly large value.   You  can  change
  491.        the size by simply #define'ing YYYYLLMMAAXX to a different value
  492.        in the first section of your  _f_l_e_x  input.   As  mentioned
  493.        above,  with  %%ppooiinntteerr yytext grows dynamically to accomo-
  494.        date large tokens.  While this means your %%ppooiinntteerr scanner
  495.        can  accomodate very large tokens (such as matching entire
  496.        blocks of comments), bear in mind that each time the scan-
  497.        ner  must  resize  yyyytteexxtt  it  also must rescan the entire
  498.        token from the beginning,  so  matching  such  tokens  can
  499.        prove slow.  yyyytteexxtt presently does _n_o_t dynamically grow if
  500.        a call to uunnppuutt(()) results in too much  text  being  pushed
  501.        back; instead, a run-time error results.
  502.  
  503.        Also  note  that  you  cannot  use %%aarrrraayy with C++ scanner
  504.        classes (the --++ option; see below).
  505.  
  506. AACCTTIIOONNSS
  507.        Each pattern in a rule has a corresponding  action,  which
  508.        can be any arbitrary C statement.  The pattern ends at the
  509.        first non-escaped whitespace character; the  remainder  of
  510.        the line is its action.  If the action is empty, then when
  511.        the pattern is matched the  input  token  is  simply  dis-
  512.        carded.  For example, here is the specification for a pro-
  513.        gram which deletes all occurrences of "zap  me"  from  its
  514.        input:
  515.  
  516.            %%
  517.            "zap me"
  518.  
  519.        (It  will  copy  all  other characters in the input to the
  520.        output since they will be matched by the default rule.)
  521.  
  522.        Here is a program which  compresses  multiple  blanks  and
  523.  
  524.  
  525.  
  526. Version 2.4               November 1993                         8
  527.  
  528.  
  529.  
  530.  
  531.  
  532. FLEXDOC(1)                                             FLEXDOC(1)
  533.  
  534.  
  535.        tabs  down  to  a single blank, and throws away whitespace
  536.        found at the end of a line:
  537.  
  538.            %%
  539.            [ \t]+        putchar( ' ' );
  540.            [ \t]+$       /* ignore this token */
  541.  
  542.  
  543.        If the action contains a '{', then the action  spans  till
  544.        the  balancing '}' is found, and the action may cross mul-
  545.        tiple lines.  _f_l_e_x knows about C strings and comments  and
  546.        won't  be  fooled  by  braces  found within them, but also
  547.        allows actions to begin with  %%{{  and  will  consider  the
  548.        action to be all the text up to the next %%}} (regardless of
  549.        ordinary braces inside the action).
  550.  
  551.        An action consisting solely of a vertical bar ('|')  means
  552.        "same  as the action for the next rule."  See below for an
  553.        illustration.
  554.  
  555.        Actions can include arbitrary  C  code,  including  rreettuurrnn
  556.        statements  to  return  a value to whatever routine called
  557.        yyyylleexx(())..  Each time yyyylleexx(()) is called  it  continues  pro-
  558.        cessing tokens from where it last left off until it either
  559.        reaches the end of the file or executes a return.
  560.  
  561.        Actions are free to modify yyyytteexxtt except  for  lengthening
  562.        it  (adding  characters  to  its end--these will overwrite
  563.        later characters in  the  input  stream).   Modifying  the
  564.        final  character of yytext may alter whether when scanning
  565.        resumes rules anchored with '^' are active.  Specifically,
  566.        changing  the  final character of yytext to a newline will
  567.        activate such rules on the next scan, and changing  it  to
  568.        anything else will deactivate the rules.  Users should not
  569.        rely on this behavior being present  in  future  releases.
  570.        Finally,  note  that  none  of this paragraph applies when
  571.        using %%aarrrraayy (see above).
  572.  
  573.        Actions are free to modify yyyylleenngg except they  should  not
  574.        do  so  if  the  action also includes use of yyyymmoorree(()) (see
  575.        below).
  576.  
  577.        There are a number of  special  directives  which  can  be
  578.        included within an action:
  579.  
  580.        -      EECCHHOO copies yytext to the scanner's output.
  581.  
  582.        -      BBEEGGIINN  followed  by  the  name of a start condition
  583.               places the scanner in the corresponding start  con-
  584.               dition (see below).
  585.  
  586.        -      RREEJJEECCTT  directs  the  scanner  to proceed on to the
  587.               "second best" rule which matched the  input  (or  a
  588.               prefix  of  the  input).   The  rule  is  chosen as
  589.  
  590.  
  591.  
  592. Version 2.4               November 1993                         9
  593.  
  594.  
  595.  
  596.  
  597.  
  598. FLEXDOC(1)                                             FLEXDOC(1)
  599.  
  600.  
  601.               described above in "How the Input is Matched",  and
  602.               yyyytteexxtt  and  yyyylleenngg  set  up appropriately.  It may
  603.               either be one which matched as  much  text  as  the
  604.               originally  chosen  rule but came later in the _f_l_e_x
  605.               input file, or one which matched  less  text.   For
  606.               example, the following will both count the words in
  607.               the input and call the routine  special()  whenever
  608.               "frob" is seen:
  609.  
  610.                           int word_count = 0;
  611.                   %%
  612.  
  613.                   frob        special(); REJECT;
  614.                   [^ \t\n]+   ++word_count;
  615.  
  616.               Without the RREEJJEECCTT,, any "frob"'s in the input would
  617.               not be counted as words, since the scanner normally
  618.               executes  only  one  action  per  token.   Multiple
  619.               RREEJJEECCTT''ss are allowed, each  one  finding  the  next
  620.               best  choice  to  the  currently  active rule.  For
  621.               example, when the following scanner scans the token
  622.               "abcd", it will write "abcdabcaba" to the output:
  623.  
  624.                   %%
  625.                   a        |
  626.                   ab       |
  627.                   abc      |
  628.                   abcd     ECHO; REJECT;
  629.                   .|\n     /* eat up any unmatched character */
  630.  
  631.               (The  first  three  rules share the fourth's action
  632.               since they use the special '|' action.)  RREEJJEECCTT  is
  633.               a  particularly  expensive feature in terms scanner
  634.               performance; if it is used in _a_n_y of the  scanner's
  635.               actions  it  will  slow  down  _a_l_l of the scanner's
  636.               matching.  Furthermore, RREEJJEECCTT cannot be used  with
  637.               the _-_C_f or _-_C_F options (see below).
  638.  
  639.               Note  also  that  unlike the other special actions,
  640.               RREEJJEECCTT is a _b_r_a_n_c_h_; code immediately  following  it
  641.               in the action will _n_o_t be executed.
  642.  
  643.        -      yyyymmoorree(())  tells  the  scanner that the next time it
  644.               matches a rule, the corresponding token  should  be
  645.               _a_p_p_e_n_d_e_d  onto  the  current value of yyyytteexxtt rather
  646.               than replacing it.  For example,  given  the  input
  647.               "mega-kludge"  the following will write "mega-mega-
  648.               kludge" to the output:
  649.  
  650.                   %%
  651.                   mega-    ECHO; yymore();
  652.                   kludge   ECHO;
  653.  
  654.               First "mega-" is matched and echoed to the  output.
  655.  
  656.  
  657.  
  658. Version 2.4               November 1993                        10
  659.  
  660.  
  661.  
  662.  
  663.  
  664. FLEXDOC(1)                                             FLEXDOC(1)
  665.  
  666.  
  667.               Then  "kludge" is matched, but the previous "mega-"
  668.               is still hanging around at the beginning of  yyyytteexxtt
  669.               so  the  EECCHHOO  for  the "kludge" rule will actually
  670.               write "mega-kludge".  The presence of  yyyymmoorree(())  in
  671.               the  scanner's  action  entails a minor performance
  672.               penalty in the scanner's matching speed.
  673.  
  674.        -      yyyylleessss((nn)) returns all but the first _n characters of
  675.               the  current  token back to the input stream, where
  676.               they will be rescanned when the scanner  looks  for
  677.               the  next  match.   yyyytteexxtt  and yyyylleenngg are adjusted
  678.               appropriately (e.g., yyyylleenngg will now be equal to  _n
  679.               ).   For example, on the input "foobar" the follow-
  680.               ing will write out "foobarbar":
  681.  
  682.                   %%
  683.                   foobar    ECHO; yyless(3);
  684.                   [a-z]+    ECHO;
  685.  
  686.               An argument of 0 to yyyylleessss will  cause  the  entire
  687.               current  input  string to be scanned again.  Unless
  688.               you've changed how the  scanner  will  subsequently
  689.               process  its input (using BBEEGGIINN,, for example), this
  690.               will result in an endless loop.
  691.  
  692.        Note that yyyylleessss is a macro and can only be  used  in  the
  693.        flex input file, not from other source files.
  694.  
  695.        -      uunnppuutt((cc))  puts  the character _c back onto the input
  696.               stream.  It will be  the  next  character  scanned.
  697.               The  following  action  will take the current token
  698.               and cause it to be rescanned enclosed in  parenthe-
  699.               ses.
  700.  
  701.                   {
  702.                   int i;
  703.                   unput( ')' );
  704.                   for ( i = yyleng - 1; i >= 0; --i )
  705.                       unput( yytext[i] );
  706.                   unput( '(' );
  707.                   }
  708.  
  709.               Note that since each uunnppuutt(()) puts the given charac-
  710.               ter back at the  _b_e_g_i_n_n_i_n_g  of  the  input  stream,
  711.               pushing  back  strings  must be done back-to-front.
  712.               Also note that you cannot put back EEOOFF  to  attempt
  713.               to mark the input stream with an end-of-file.
  714.  
  715.        -      iinnppuutt(())  reads  the  next  character from the input
  716.               stream.  For example, the following is one  way  to
  717.               eat up C comments:
  718.  
  719.                   %%
  720.                   "/*"        {
  721.  
  722.  
  723.  
  724. Version 2.4               November 1993                        11
  725.  
  726.  
  727.  
  728.  
  729.  
  730. FLEXDOC(1)                                             FLEXDOC(1)
  731.  
  732.  
  733.                               register int c;
  734.  
  735.                               for ( ; ; )
  736.                                   {
  737.                                   while ( (c = input()) != '*' &&
  738.                                           c != EOF )
  739.                                       ;    /* eat up text of comment */
  740.  
  741.                                   if ( c == '*' )
  742.                                       {
  743.                                       while ( (c = input()) == '*' )
  744.                                           ;
  745.                                       if ( c == '/' )
  746.                                           break;    /* found the end */
  747.                                       }
  748.  
  749.                                   if ( c == EOF )
  750.                                       {
  751.                                       error( "EOF in comment" );
  752.                                       break;
  753.                                       }
  754.                                   }
  755.                               }
  756.  
  757.               (Note  that  if  the scanner is compiled using CC++++,,
  758.               then iinnppuutt(()) is instead referred to  as  yyyyiinnppuutt(()),,
  759.               in  order to avoid a name clash with the CC++++ stream
  760.               by the name of _i_n_p_u_t_._)
  761.  
  762.        -      yyyytteerrmmiinnaattee(()) can be  used  in  lieu  of  a  return
  763.               statement  in an action.  It terminates the scanner
  764.               and returns a 0 to the scanner's caller, indicating
  765.               "all  done".   By  default,  yyyytteerrmmiinnaattee(())  is also
  766.               called when an end-of-file is encountered.  It is a
  767.               macro and may be redefined.
  768.  
  769. TTHHEE GGEENNEERRAATTEEDD SSCCAANNNNEERR
  770.        The  output  of  _f_l_e_x is the file lleexx..yyyy..cc,, which contains
  771.        the scanning routine yyyylleexx(()),, a number of tables  used  by
  772.        it for matching tokens, and a number of auxiliary routines
  773.        and macros.  By default, yyyylleexx(()) is declared as follows:
  774.  
  775.            int yylex()
  776.                {
  777.                ... various definitions and the actions in here ...
  778.                }
  779.  
  780.        (If your environment supports function prototypes, then it
  781.        will  be  "int  yylex(  void  )".)  This definition may be
  782.        changed by defining the "YY_DECL" macro.  For example, you
  783.        could use:
  784.  
  785.            #define YY_DECL float lexscan( a, b ) float a, b;
  786.  
  787.  
  788.  
  789.  
  790. Version 2.4               November 1993                        12
  791.  
  792.  
  793.  
  794.  
  795.  
  796. FLEXDOC(1)                                             FLEXDOC(1)
  797.  
  798.  
  799.        to give the scanning routine the name _l_e_x_s_c_a_n_, returning a
  800.        float, and taking two floats as arguments.  Note  that  if
  801.        you  give  arguments  to the scanning routine using a K&R-
  802.        style/non-prototyped function declaration, you must termi-
  803.        nate the definition with a semi-colon (;).
  804.  
  805.        Whenever  yyyylleexx(())  is  called,  it  scans  tokens from the
  806.        global input file _y_y_i_n (which defaults to stdin).  It con-
  807.        tinues  until  it  either reaches an end-of-file (at which
  808.        point it returns the value 0) or one of its  actions  exe-
  809.        cutes a _r_e_t_u_r_n statement.
  810.  
  811.        If  the  scanner  reaches an end-of-file, subsequent calls
  812.        are undefined unless either _y_y_i_n is pointed at a new input
  813.        file (in which case scanning continues from that file), or
  814.        yyyyrreessttaarrtt(()) is called.  yyyyrreessttaarrtt(()) takes one argument,  a
  815.        FFIILLEE  **  pointer,  and  initializes _y_y_i_n for scanning from
  816.        that file.  Essentially there  is  no  difference  between
  817.        just   assigning  _y_y_i_n  to  a  new  input  file  or  using
  818.        yyyyrreessttaarrtt(()) to do so; the latter is available for compati-
  819.        bility  with previous versions of _f_l_e_x_, and because it can
  820.        be used to switch input files in the middle  of  scanning.
  821.        It  can  also  be  used  to  throw  away the current input
  822.        buffer, by calling it with an argument of _y_y_i_n_.
  823.  
  824.        If yyyylleexx(()) stops scanning due to executing a _r_e_t_u_r_n state-
  825.        ment in one of the actions, the scanner may then be called
  826.        again and it will resume scanning where it left off.
  827.  
  828.        By default (and for purposes of efficiency),  the  scanner
  829.        uses  block-reads  rather than simple _g_e_t_c_(_) calls to read
  830.        characters from _y_y_i_n_.  The nature of how it gets its input
  831.        can   be   controlled  by  defining  the  YYYY__IINNPPUUTT  macro.
  832.        YY_INPUT's          calling          sequence           is
  833.        "YY_INPUT(buf,result,max_size)".   Its  action is to place
  834.        up to _m_a_x___s_i_z_e characters in the character array  _b_u_f  and
  835.        return in the integer variable _r_e_s_u_l_t either the number of
  836.        characters read or the constant YY_NULL (0  on  Unix  sys-
  837.        tems)  to  indicate  EOF.  The default YY_INPUT reads from
  838.        the global file-pointer "yyin".
  839.  
  840.        A sample definition of YY_INPUT (in the  definitions  sec-
  841.        tion of the input file):
  842.  
  843.            %{
  844.            #define YY_INPUT(buf,result,max_size) \
  845.                { \
  846.                int c = getchar(); \
  847.                result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); \
  848.                }
  849.            %}
  850.  
  851.        This  definition will change the input processing to occur
  852.        one character at a time.
  853.  
  854.  
  855.  
  856. Version 2.4               November 1993                        13
  857.  
  858.  
  859.  
  860.  
  861.  
  862. FLEXDOC(1)                                             FLEXDOC(1)
  863.  
  864.  
  865.        You also can add in things like keeping track of the input
  866.        line  number this way; but don't expect your scanner to go
  867.        very fast.
  868.  
  869.        When the scanner receives an end-of-file  indication  from
  870.        YY_INPUT,  it  then  checks  the  yyyywwrraapp(())  function.   If
  871.        yyyywwrraapp(()) returns false (zero), then it is assumed that the
  872.        function  has  gone  ahead  and  set  up  _y_y_i_n to point to
  873.        another input file, and scanning continues.  If it returns
  874.        true  (non-zero), then the scanner terminates, returning 0
  875.        to its caller.
  876.  
  877.        The default yyyywwrraapp(()) always returns 1.
  878.  
  879.        The scanner writes its EECCHHOO output  to  the  _y_y_o_u_t  global
  880.        (default, stdout), which may be redefined by the user sim-
  881.        ply by assigning it to some other FFIILLEE pointer.
  882.  
  883. SSTTAARRTT CCOONNDDIITTIIOONNSS
  884.        _f_l_e_x provides a  mechanism  for  conditionally  activating
  885.        rules.   Any  rule  whose  pattern is prefixed with "<sc>"
  886.        will only be active when the scanner is in the start  con-
  887.        dition named "sc".  For example,
  888.  
  889.            <STRING>[^"]*        { /* eat up the string body ... */
  890.                        ...
  891.                        }
  892.  
  893.        will  be  active  only when the scanner is in the "STRING"
  894.        start condition, and
  895.  
  896.            <INITIAL,STRING,QUOTE>\.        { /* handle an escape ... */
  897.                        ...
  898.                        }
  899.  
  900.        will be active only when the current  start  condition  is
  901.        either "INITIAL", "STRING", or "QUOTE".
  902.  
  903.        Start  conditions  are declared in the definitions (first)
  904.        section of the input using unindented lines beginning with
  905.        either  %%ss  or %%xx followed by a list of names.  The former
  906.        declares _i_n_c_l_u_s_i_v_e start conditions, the latter  _e_x_c_l_u_s_i_v_e
  907.        start  conditions.   A  start condition is activated using
  908.        the BBEEGGIINN action.  Until the next  BBEEGGIINN  action  is  exe-
  909.        cuted, rules with the given start condition will be active
  910.        and rules with other start conditions  will  be  inactive.
  911.        If  the  start  condition is _i_n_c_l_u_s_i_v_e_, then rules with no
  912.        start conditions at all will also be  active.   If  it  is
  913.        _e_x_c_l_u_s_i_v_e_, then _o_n_l_y rules qualified with the start condi-
  914.        tion will be active.  A set of  rules  contingent  on  the
  915.        same exclusive start condition describe a scanner which is
  916.        independent of any of the other rules in the  _f_l_e_x  input.
  917.        Because  of  this, exclusive start conditions make it easy
  918.        to specify "mini-scanners"  which  scan  portions  of  the
  919.  
  920.  
  921.  
  922. Version 2.4               November 1993                        14
  923.  
  924.  
  925.  
  926.  
  927.  
  928. FLEXDOC(1)                                             FLEXDOC(1)
  929.  
  930.  
  931.        input  that  are  syntactically  different  from  the rest
  932.        (e.g., comments).
  933.  
  934.        If the distinction between inclusive and  exclusive  start
  935.        conditions  is still a little vague, here's a simple exam-
  936.        ple illustrating the connection between the two.  The  set
  937.        of rules:
  938.  
  939.            %s example
  940.            %%
  941.            <example>foo           /* do something */
  942.  
  943.        is equivalent to
  944.  
  945.            %x example
  946.            %%
  947.            <INITIAL,example>foo   /* do something */
  948.  
  949.  
  950.        Also  note  that the special start-condition specifier <<**>>
  951.        matches every start condition.  Thus,  the  above  example
  952.        could also have been written;
  953.  
  954.            %x example
  955.            %%
  956.            <*>foo   /* do something */
  957.  
  958.  
  959.        The default rule (to EECCHHOO any unmatched character) remains
  960.        active in start conditions.
  961.  
  962.        BBEEGGIINN((00)) returns to the  original  state  where  only  the
  963.        rules with no start conditions are active.  This state can
  964.        also be referred to as the start-condition  "INITIAL",  so
  965.        BBEEGGIINN((IINNIITTIIAALL))  is equivalent to BBEEGGIINN((00))..  (The parenthe-
  966.        ses around the start condition name are not  required  but
  967.        are considered good style.)
  968.  
  969.        BBEEGGIINN  actions  can  also be given as indented code at the
  970.        beginning of the rules section.  For example, the  follow-
  971.        ing  will  cause  the scanner to enter the "SPECIAL" start
  972.        condition whenever _y_y_l_e_x_(_) is called and the global  vari-
  973.        able _e_n_t_e_r___s_p_e_c_i_a_l is true:
  974.  
  975.                    int enter_special;
  976.  
  977.            %x SPECIAL
  978.            %%
  979.                    if ( enter_special )
  980.                        BEGIN(SPECIAL);
  981.  
  982.            <SPECIAL>blahblahblah
  983.            ...more rules follow...
  984.  
  985.  
  986.  
  987.  
  988. Version 2.4               November 1993                        15
  989.  
  990.  
  991.  
  992.  
  993.  
  994. FLEXDOC(1)                                             FLEXDOC(1)
  995.  
  996.  
  997.        To  illustrate  the  uses  of  start conditions, here is a
  998.        scanner which provides two different interpretations of  a
  999.        string  like "123.456".  By default it will treat it as as
  1000.        three tokens, the integer "123",  a  dot  ('.'),  and  the
  1001.        integer  "456".   But if the string is preceded earlier in
  1002.        the line by the string "expect-floats" it will treat it as
  1003.        a single token, the floating-point number 123.456:
  1004.  
  1005.            %{
  1006.            #include <math.h>
  1007.            %}
  1008.            %s expect
  1009.  
  1010.            %%
  1011.            expect-floats        BEGIN(expect);
  1012.  
  1013.            <expect>[0-9]+"."[0-9]+      {
  1014.                        printf( "found a float, = %f\n",
  1015.                                atof( yytext ) );
  1016.                        }
  1017.            <expect>\n           {
  1018.                        /* that's the end of the line, so
  1019.                         * we need another "expect-number"
  1020.                         * before we'll recognize any more
  1021.                         * numbers
  1022.                         */
  1023.                        BEGIN(INITIAL);
  1024.                        }
  1025.  
  1026.            [0-9]+      {
  1027.                        printf( "found an integer, = %d\n",
  1028.                                atoi( yytext ) );
  1029.                        }
  1030.  
  1031.            "."         printf( "found a dot\n" );
  1032.  
  1033.        Here  is  a scanner which recognizes (and discards) C com-
  1034.        ments while maintaining a count of the current input line.
  1035.  
  1036.            %x comment
  1037.            %%
  1038.                    int line_num = 1;
  1039.  
  1040.            "/*"         BEGIN(comment);
  1041.  
  1042.            <comment>[^*\n]*        /* eat anything that's not a '*' */
  1043.            <comment>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
  1044.            <comment>\n             ++line_num;
  1045.            <comment>"*"+"/"        BEGIN(INITIAL);
  1046.  
  1047.        This  scanner  goes  to  a bit of trouble to match as much
  1048.        text  as  possible  with  each  rule.   In  general,  when
  1049.        attempting  to  write a high-speed scanner try to match as
  1050.        much possible in each rule, as it's a big win.
  1051.  
  1052.  
  1053.  
  1054. Version 2.4               November 1993                        16
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060. FLEXDOC(1)                                             FLEXDOC(1)
  1061.  
  1062.  
  1063.        Note that start-conditions names are really integer values
  1064.        and  can  be  stored  as  such.   Thus, the above could be
  1065.        extended in the following fashion:
  1066.  
  1067.            %x comment foo
  1068.            %%
  1069.                    int line_num = 1;
  1070.                    int comment_caller;
  1071.  
  1072.            "/*"         {
  1073.                         comment_caller = INITIAL;
  1074.                         BEGIN(comment);
  1075.                         }
  1076.  
  1077.            ...
  1078.  
  1079.            <foo>"/*"    {
  1080.                         comment_caller = foo;
  1081.                         BEGIN(comment);
  1082.                         }
  1083.  
  1084.            <comment>[^*\n]*        /* eat anything that's not a '*' */
  1085.            <comment>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
  1086.            <comment>\n             ++line_num;
  1087.            <comment>"*"+"/"        BEGIN(comment_caller);
  1088.  
  1089.        Furthermore, you can access the  current  start  condition
  1090.        using the integer-valued YYYY__SSTTAARRTT macro.  For example, the
  1091.        above assignments to _c_o_m_m_e_n_t___c_a_l_l_e_r could instead be writ-
  1092.        ten
  1093.  
  1094.            comment_caller = YY_START;
  1095.  
  1096.        Note  that  start  conditions  do not have their own name-
  1097.        space; %s's and %x's declare names in the same fashion  as
  1098.        #define's.
  1099.  
  1100.        Finally,  here's an example of how to match C-style quoted
  1101.        strings  using  exclusive  start   conditions,   including
  1102.        expanded  escape sequences (but not including checking for
  1103.        a string that's too long):
  1104.  
  1105.            %x str
  1106.  
  1107.            %%
  1108.                    char string_buf[MAX_STR_CONST];
  1109.                    char *string_buf_ptr;
  1110.  
  1111.  
  1112.            \"      string_buf_ptr = string_buf; BEGIN(str);
  1113.  
  1114.            <str>\"        { /* saw closing quote - all done */
  1115.                    BEGIN(INITIAL);
  1116.                    *string_buf_ptr = '\0';
  1117.  
  1118.  
  1119.  
  1120. Version 2.4               November 1993                        17
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126. FLEXDOC(1)                                             FLEXDOC(1)
  1127.  
  1128.  
  1129.                    /* return string constant token type and
  1130.                     * value to parser
  1131.                     */
  1132.                    }
  1133.  
  1134.            <str>\n        {
  1135.                    /* error - unterminated string constant */
  1136.                    /* generate error message */
  1137.                    }
  1138.  
  1139.            <str>\\[0-7]{1,3} {
  1140.                    /* octal escape sequence */
  1141.                    int result;
  1142.  
  1143.                    (void) sscanf( yytext + 1, "%o", &result );
  1144.  
  1145.                    if ( result > 0xff )
  1146.                            /* error, constant is out-of-bounds */
  1147.  
  1148.                    *string_buf_ptr++ = result;
  1149.                    }
  1150.  
  1151.            <str>\\[0-9]+ {
  1152.                    /* generate error - bad escape sequence; something
  1153.                     * like '\48' or '\0777777'
  1154.                     */
  1155.                    }
  1156.  
  1157.            <str>\\n  *string_buf_ptr++ = '\n';
  1158.            <str>\\t  *string_buf_ptr++ = '\t';
  1159.            <str>\\r  *string_buf_ptr++ = '\r';
  1160.            <str>\\b  *string_buf_ptr++ = '\b';
  1161.            <str>\\f  *string_buf_ptr++ = '\f';
  1162.  
  1163.            <str>\\(.|\n)  *string_buf_ptr++ = yytext[1];
  1164.  
  1165.            <str>[^\\\n\"]+        {
  1166.                    char *yytext_ptr = yytext;
  1167.  
  1168.                    while ( *yytext_ptr )
  1169.                            *string_buf_ptr++ = *yytext_ptr++;
  1170.                    }
  1171.  
  1172.  
  1173. MMUULLTTIIPPLLEE IINNPPUUTT BBUUFFFFEERRSS
  1174.        Some scanners  (such  as  those  which  support  "include"
  1175.        files)  require  reading  from  several input streams.  As
  1176.        _f_l_e_x scanners do a large amount of buffering,  one  cannot
  1177.        control  where  the next input will be read from by simply
  1178.        writing a YYYY__IINNPPUUTT which is sensitive to the scanning con-
  1179.        text.   YYYY__IINNPPUUTT  is  only called when the scanner reaches
  1180.        the end of its buffer, which may  be  a  long  time  after
  1181.        scanning  a  statement such as an "include" which requires
  1182.        switching the input source.
  1183.  
  1184.  
  1185.  
  1186. Version 2.4               November 1993                        18
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192. FLEXDOC(1)                                             FLEXDOC(1)
  1193.  
  1194.  
  1195.        To negotiate these sorts  of  problems,  _f_l_e_x  provides  a
  1196.        mechanism  for  creating  and  switching  between multiple
  1197.        input buffers.  An input buffer is created by using:
  1198.  
  1199.            YY_BUFFER_STATE yy_create_buffer( FILE *file, int size )
  1200.  
  1201.        which takes a _F_I_L_E pointer and a size and creates a buffer
  1202.        associated  with  the  given file and large enough to hold
  1203.        _s_i_z_e characters (when in doubt, use  YYYY__BBUUFF__SSIIZZEE  for  the
  1204.        size).   It  returns  a  YYYY__BBUUFFFFEERR__SSTTAATTEE handle, which may
  1205.        then be passed to other routines:
  1206.  
  1207.            void yy_switch_to_buffer( YY_BUFFER_STATE new_buffer )
  1208.  
  1209.        switches the scanner's input buffer so  subsequent  tokens
  1210.        will      come     from     _n_e_w___b_u_f_f_e_r_.      Note     that
  1211.        yyyy__sswwiittcchh__ttoo__bbuuffffeerr(()) may  be  used  by  yywrap()  to  set
  1212.        things up for continued scanning, instead of opening a new
  1213.        file and pointing _y_y_i_n at it.
  1214.  
  1215.            void yy_delete_buffer( YY_BUFFER_STATE buffer )
  1216.  
  1217.        is used to reclaim the storage associated with a buffer.
  1218.  
  1219.        yyyy__nneeww__bbuuffffeerr(()) is an alias for  yyyy__ccrreeaattee__bbuuffffeerr(()),,  pro-
  1220.        vided for compatibility with the C++ use of _n_e_w and _d_e_l_e_t_e
  1221.        for creating and destroying dynamic objects.
  1222.  
  1223.        Finally,   the   YYYY__CCUURRRREENNTT__BBUUFFFFEERR   macro    returns    a
  1224.        YYYY__BBUUFFFFEERR__SSTTAATTEE handle to the current buffer.
  1225.  
  1226.        Here  is  an example of using these features for writing a
  1227.        scanner which expands include files (the  <<<<EEOOFF>>>>  feature
  1228.        is discussed below):
  1229.  
  1230.            /* the "incl" state is used for picking up the name
  1231.             * of an include file
  1232.             */
  1233.            %x incl
  1234.  
  1235.            %{
  1236.            #define MAX_INCLUDE_DEPTH 10
  1237.            YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
  1238.            int include_stack_ptr = 0;
  1239.            %}
  1240.  
  1241.            %%
  1242.            include             BEGIN(incl);
  1243.  
  1244.            [a-z]+              ECHO;
  1245.            [^a-z\n]*\n?        ECHO;
  1246.  
  1247.            <incl>[ \t]*      /* eat the whitespace */
  1248.            <incl>[^ \t\n]+   { /* got the include file name */
  1249.  
  1250.  
  1251.  
  1252. Version 2.4               November 1993                        19
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258. FLEXDOC(1)                                             FLEXDOC(1)
  1259.  
  1260.  
  1261.                    if ( include_stack_ptr >= MAX_INCLUDE_DEPTH )
  1262.                        {
  1263.                        fprintf( stderr, "Includes nested too deeply" );
  1264.                        exit( 1 );
  1265.                        }
  1266.  
  1267.                    include_stack[include_stack_ptr++] =
  1268.                        YY_CURRENT_BUFFER;
  1269.  
  1270.                    yyin = fopen( yytext, "r" );
  1271.  
  1272.                    if ( ! yyin )
  1273.                        error( ... );
  1274.  
  1275.                    yy_switch_to_buffer(
  1276.                        yy_create_buffer( yyin, YY_BUF_SIZE ) );
  1277.  
  1278.                    BEGIN(INITIAL);
  1279.                    }
  1280.  
  1281.            <<EOF>> {
  1282.                    if ( --include_stack_ptr < 0 )
  1283.                        {
  1284.                        yyterminate();
  1285.                        }
  1286.  
  1287.                    else
  1288.                        {
  1289.                        yy_delete_buffer( YY_CURRENT_BUFFER );
  1290.                        yy_switch_to_buffer(
  1291.                             include_stack[include_stack_ptr] );
  1292.                        }
  1293.                    }
  1294.  
  1295.  
  1296. EENNDD--OOFF--FFIILLEE RRUULLEESS
  1297.        The  special rule "<<EOF>>" indicates actions which are to
  1298.        be taken when an end-of-file is encountered  and  yywrap()
  1299.        returns non-zero (i.e., indicates no further files to pro-
  1300.        cess).  The action  must  finish  by  doing  one  of  four
  1301.        things:
  1302.  
  1303.        -      assigning  _y_y_i_n  to  a  new input file (in previous
  1304.               versions of flex, after doing  the  assignment  you
  1305.               had to call the special action YYYY__NNEEWW__FFIILLEE;; this is
  1306.               no longer necessary);
  1307.  
  1308.        -      executing a _r_e_t_u_r_n statement;
  1309.  
  1310.        -      executing the special yyyytteerrmmiinnaattee(()) action;
  1311.  
  1312.        -      or,   switching   to    a    new    buffer    using
  1313.               yyyy__sswwiittcchh__ttoo__bbuuffffeerr(())   as  shown  in  the  example
  1314.               above.
  1315.  
  1316.  
  1317.  
  1318. Version 2.4               November 1993                        20
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324. FLEXDOC(1)                                             FLEXDOC(1)
  1325.  
  1326.  
  1327.        <<EOF>> rules may not be used with  other  patterns;  they
  1328.        may only be qualified with a list of start conditions.  If
  1329.        an unqualified <<EOF>> rule is given, it  applies  to  _a_l_l
  1330.        start   conditions  which  do  not  already  have  <<EOF>>
  1331.        actions.  To specify an <<EOF>> rule for only the  initial
  1332.        start condition, use
  1333.  
  1334.            <INITIAL><<EOF>>
  1335.  
  1336.  
  1337.        These  rules  are useful for catching things like unclosed
  1338.        comments.  An example:
  1339.  
  1340.            %x quote
  1341.            %%
  1342.  
  1343.            ...other rules for dealing with quotes...
  1344.  
  1345.            <quote><<EOF>>   {
  1346.                     error( "unterminated quote" );
  1347.                     yyterminate();
  1348.                     }
  1349.            <<EOF>>  {
  1350.                     if ( *++filelist )
  1351.                         yyin = fopen( *filelist, "r" );
  1352.                     else
  1353.                        yyterminate();
  1354.                     }
  1355.  
  1356.  
  1357. MMIISSCCEELLLLAANNEEOOUUSS MMAACCRROOSS
  1358.        The macro YY_USER_ACTION can  be  defined  to  provide  an
  1359.        action  which  is  always  executed  prior  to the matched
  1360.        rule's action.  For example, it could be #define'd to call
  1361.        a routine to convert yytext to lower-case.
  1362.  
  1363.        The macro YYYY__UUSSEERR__IINNIITT may be defined to provide an action
  1364.        which is always executed before the first scan (and before
  1365.        the  scanner's  internal  initializations  are done).  For
  1366.        example, it could be used to call a routine to read  in  a
  1367.        data table or open a logging file.
  1368.  
  1369.        In  the generated scanner, the actions are all gathered in
  1370.        one large switch statement and separated  using  YYYY__BBRREEAAKK,,
  1371.        which  may  be  redefined.   By  default,  it  is simply a
  1372.        "break", to separate each rule's action from the following
  1373.        rule's.   Redefining  YYYY__BBRREEAAKK  allows,  for  example, C++
  1374.        users to #define YY_BREAK to do nothing (while being  very
  1375.        careful   that  every  rule  ends  with  a  "break"  or  a
  1376.        "return"!) to avoid suffering from  unreachable  statement
  1377.        warnings where because a rule's action ends with "return",
  1378.        the YYYY__BBRREEAAKK is inaccessible.
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384. Version 2.4               November 1993                        21
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390. FLEXDOC(1)                                             FLEXDOC(1)
  1391.  
  1392.  
  1393. IINNTTEERRFFAACCIINNGG WWIITTHH YYAACCCC
  1394.        One of the main uses of _f_l_e_x is as a companion to the _y_a_c_c
  1395.        parser-generator.   _y_a_c_c  parsers expect to call a routine
  1396.        named yyyylleexx(()) to find the next input token.   The  routine
  1397.        is  supposed  to return the type of the next token as well
  1398.        as putting any associated value in the global yyyyllvvaall..   To
  1399.        use _f_l_e_x with _y_a_c_c_, one specifies the --dd option to _y_a_c_c to
  1400.        instruct it to generate the file yy..ttaabb..hh containing  defi-
  1401.        nitions  of  all  the %%ttookkeennss appearing in the _y_a_c_c input.
  1402.        This file is then included in the _f_l_e_x scanner.  For exam-
  1403.        ple,  if  one  of  the tokens is "TOK_NUMBER", part of the
  1404.        scanner might look like:
  1405.  
  1406.            %{
  1407.            #include "y.tab.h"
  1408.            %}
  1409.  
  1410.            %%
  1411.  
  1412.            [0-9]+        yylval = atoi( yytext ); return TOK_NUMBER;
  1413.  
  1414.  
  1415. OOPPTTIIOONNSS
  1416.        _f_l_e_x has the following options:
  1417.  
  1418.        --bb     Generate  backing-up  information  to   _l_e_x_._b_a_c_k_u_p_.
  1419.               This  is  a  list  of  scanner states which require
  1420.               backing up and the input characters on  which  they
  1421.               do  so.   By adding rules one can remove backing-up
  1422.               states.  If all backing-up  states  are  eliminated
  1423.               and  --CCff or --CCFF is used, the generated scanner will
  1424.               run faster (see the --pp flag).  Only users who  wish
  1425.               to  squeeze  every last cycle out of their scanners
  1426.               need worry about this option.  (See the section  on
  1427.               Performance Considerations below.)
  1428.  
  1429.        --cc     is  a  do-nothing,  deprecated  option included for
  1430.               POSIX compliance.
  1431.  
  1432.               NNOOTTEE:: in previous releases  of  _f_l_e_x  --cc  specified
  1433.               table-compression  options.   This functionality is
  1434.               now given by the --CC flag.  To ease the  the  impact
  1435.               of  this  change,  when _f_l_e_x encounters --cc,, it cur-
  1436.               rently issues a warning message and assumes that --CC
  1437.               was  desired  instead.   In the future this "promo-
  1438.               tion" of --cc to --CC will go away in the name of  full
  1439.               POSIX  compliance  (unless  the  POSIX  meaning  is
  1440.               removed first).
  1441.  
  1442.        --dd     makes the generated  scanner  run  in  _d_e_b_u_g  mode.
  1443.               Whenever  a  pattern  is  recognized and the global
  1444.               yyyy__fflleexx__ddeebbuugg is non-zero (which is  the  default),
  1445.               the  scanner  will  write  to  _s_t_d_e_r_r a line of the
  1446.               form:
  1447.  
  1448.  
  1449.  
  1450. Version 2.4               November 1993                        22
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456. FLEXDOC(1)                                             FLEXDOC(1)
  1457.  
  1458.  
  1459.                   --accepting rule at line 53 ("the matched text")
  1460.  
  1461.               The line number refers to the location of the  rule
  1462.               in  the  file  defining the scanner (i.e., the file
  1463.               that was fed to flex).  Messages are also generated
  1464.               when  the  scanner  backs  up,  accepts the default
  1465.               rule, reaches the  end  of  its  input  buffer  (or
  1466.               encounters  a  NUL; at this point, the two look the
  1467.               same as far as the scanner's concerned), or reaches
  1468.               an end-of-file.
  1469.  
  1470.        --ff     specifies  _f_a_s_t  _s_c_a_n_n_e_r_.   No table compression is
  1471.               done and stdio is bypassed.  The  result  is  large
  1472.               but  fast.   This option is equivalent to --CCffrr (see
  1473.               below).
  1474.  
  1475.        --hh     generates a "help" summary  of  _f_l_e_x_'_s  options  to
  1476.               _s_t_d_e_r_r and then exits.
  1477.  
  1478.        --ii     instructs _f_l_e_x to generate a _c_a_s_e_-_i_n_s_e_n_s_i_t_i_v_e scan-
  1479.               ner.  The case of letters given in the  _f_l_e_x  input
  1480.               patterns  will  be ignored, and tokens in the input
  1481.               will be matched regardless of  case.   The  matched
  1482.               text  given  in _y_y_t_e_x_t will have the preserved case
  1483.               (i.e., it will not be folded).
  1484.  
  1485.        --ll     turns on maximum compatibility  with  the  original
  1486.               AT&T  _l_e_x  implementation.  Note that this does not
  1487.               mean _f_u_l_l compatibility.  Use of this option  costs
  1488.               a considerable amount of performance, and it cannot
  1489.               be used with the --++,, --ff,, --FF,, --CCff,, or  --CCFF  options.
  1490.               For details on the compatibilities it provides, see
  1491.               the section "Incompatibilities With Lex And  POSIX"
  1492.               below.
  1493.  
  1494.        --nn     is  another  do-nothing, deprecated option included
  1495.               only for POSIX compliance.
  1496.  
  1497.        --pp     generates a  performance  report  to  stderr.   The
  1498.               report  consists  of comments regarding features of
  1499.               the _f_l_e_x input file which will cause a serious loss
  1500.               of  performance  in  the resulting scanner.  If you
  1501.               give the flag twice, you  will  also  get  comments
  1502.               regarding  features  that lead to minor performance
  1503.               losses.
  1504.  
  1505.               Note that the use of RREEJJEECCTT and  variable  trailing
  1506.               context (see the Bugs section in flex(1)) entails a
  1507.               substantial performance penalty; use  of  _y_y_m_o_r_e_(_)_,
  1508.               the  ^^  operator, and the --II flag entail minor per-
  1509.               formance penalties.
  1510.  
  1511.        --ss     causes the _d_e_f_a_u_l_t  _r_u_l_e  (that  unmatched  scanner
  1512.               input  is  echoed  to _s_t_d_o_u_t_) to be suppressed.  If
  1513.  
  1514.  
  1515.  
  1516. Version 2.4               November 1993                        23
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522. FLEXDOC(1)                                             FLEXDOC(1)
  1523.  
  1524.  
  1525.               the scanner encounters input that  does  not  match
  1526.               any  of  its  rules, it aborts with an error.  This
  1527.               option is useful for finding holes in  a  scanner's
  1528.               rule set.
  1529.  
  1530.        --tt     instructs _f_l_e_x to write the scanner it generates to
  1531.               standard output instead of lleexx..yyyy..cc..
  1532.  
  1533.        --vv     specifies that _f_l_e_x should write to _s_t_d_e_r_r  a  sum-
  1534.               mary  of statistics regarding the scanner it gener-
  1535.               ates.  Most of the statistics  are  meaningless  to
  1536.               the casual _f_l_e_x user, but the first line identifies
  1537.               the version of _f_l_e_x (same as reported by  --VV)),,  and
  1538.               the  next  line  the flags used when generating the
  1539.               scanner, including those that are on by default.
  1540.  
  1541.        --ww     suppresses warning messages.
  1542.  
  1543.        --BB     instructs _f_l_e_x to generate  a  _b_a_t_c_h  scanner,  the
  1544.               opposite  of  _i_n_t_e_r_a_c_t_i_v_e  scanners generated by --II
  1545.               (see below).  In general, you use --BB when  you  are
  1546.               _c_e_r_t_a_i_n that your scanner will never be used inter-
  1547.               actively, and you want to  squeeze  a  _l_i_t_t_l_e  more
  1548.               performance  out of it.  If your goal is instead to
  1549.               squeeze out a _l_o_t more performance, you should   be
  1550.               using  the  --CCff  or  --CCFF options (discussed below),
  1551.               which turn on --BB automatically anyway.
  1552.  
  1553.        --FF     specifies that the _f_a_s_t scanner  table  representa-
  1554.               tion  should  be  used  (and stdio bypassed).  This
  1555.               representation is about as fast as the  full  table
  1556.               representation  ((--ff)),, and for some sets of patterns
  1557.               will  be  considerably  smaller  (and  for  others,
  1558.               larger).   In  general, if the pattern set contains
  1559.               both "keywords" and a catch-all, "identifier" rule,
  1560.               such as in the set:
  1561.  
  1562.                   "case"    return TOK_CASE;
  1563.                   "switch"  return TOK_SWITCH;
  1564.                   ...
  1565.                   "default" return TOK_DEFAULT;
  1566.                   [a-z]+    return TOK_ID;
  1567.  
  1568.               then  you're better off using the full table repre-
  1569.               sentation.  If only the "identifier" rule  is  pre-
  1570.               sent  and you then use a hash table or some such to
  1571.               detect the keywords, you're better off using --FF..
  1572.  
  1573.               This option is equivalent to --CCFFrr (see below).   It
  1574.               cannot be used with --++..
  1575.  
  1576.        --II     instructs  _f_l_e_x to generate an _i_n_t_e_r_a_c_t_i_v_e scanner.
  1577.               An interactive scanner is one that only looks ahead
  1578.               to  decide  what  token  has  been  matched  if  it
  1579.  
  1580.  
  1581.  
  1582. Version 2.4               November 1993                        24
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588. FLEXDOC(1)                                             FLEXDOC(1)
  1589.  
  1590.  
  1591.               absolutely must.  It turns out that always  looking
  1592.               one  extra character ahead, even if the scanner has
  1593.               already seen enough text to disambiguate  the  cur-
  1594.               rent token, is a bit faster than only looking ahead
  1595.               when necessary.   But  scanners  that  always  look
  1596.               ahead  give  dreadful  interactive performance; for
  1597.               example, when a user types a  newline,  it  is  not
  1598.               recognized  as  a  newline  token  until they enter
  1599.               _a_n_o_t_h_e_r token, which often means typing in  another
  1600.               whole line.
  1601.  
  1602.               _F_l_e_x scanners default to _i_n_t_e_r_a_c_t_i_v_e unless you use
  1603.               the  --CCff  or  --CCFF  table-compression  options  (see
  1604.               below).  That's because if you're looking for high-
  1605.               performance  you  should  be  using  one  of  these
  1606.               options,  so  if  you  didn't,  _f_l_e_x  assumes you'd
  1607.               rather trade off a bit of run-time performance  for
  1608.               intuitive interactive behavior.  Note also that you
  1609.               _c_a_n_n_o_t use --II  in  conjunction  with  --CCff  or  --CCFF..
  1610.               Thus, this option is not really needed; it is on by
  1611.               default for all those cases in which it is allowed.
  1612.  
  1613.               You  can  force  a scanner to _n_o_t be interactive by
  1614.               using --BB (see above).
  1615.  
  1616.        --LL     instructs _f_l_e_x not to  generate  ##lliinnee  directives.
  1617.               Without  this  option,  _f_l_e_x  peppers the generated
  1618.               scanner with #line directives so error messages  in
  1619.               the  actions will be correctly located with respect
  1620.               to the original _f_l_e_x input file,  and  not  to  the
  1621.               fairly   meaningless   line  numbers  of  lleexx..yyyy..cc..
  1622.               (Unfortunately _f_l_e_x does not presently generate the
  1623.               necessary directives to "retarget" the line numbers
  1624.               for those parts of lleexx..yyyy..cc which it generated.  So
  1625.               if there is an error in the generated code, a mean-
  1626.               ingless line number is reported.)
  1627.  
  1628.        --TT     makes _f_l_e_x run in _t_r_a_c_e mode.  It will  generate  a
  1629.               lot  of  messages  to _s_t_d_e_r_r concerning the form of
  1630.               the input and the resultant  non-deterministic  and
  1631.               deterministic  finite  automata.   This  option  is
  1632.               mostly for use in maintaining _f_l_e_x_.
  1633.  
  1634.        --VV     prints the version number to _s_t_d_e_r_r and exits.
  1635.  
  1636.        --77     instructs _f_l_e_x to generate a 7-bit  scanner,  i.e.,
  1637.               one  which  can only recognized 7-bit characters in
  1638.               its input.  The advantage of using --77 is  that  the
  1639.               scanner's  tables  can  be  up  to half the size of
  1640.               those generated using the --88  option  (see  below).
  1641.               The  disadvantage  is that such scanners often hang
  1642.               or crash if their input contains an  8-bit  charac-
  1643.               ter.
  1644.  
  1645.  
  1646.  
  1647.  
  1648. Version 2.4               November 1993                        25
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654. FLEXDOC(1)                                             FLEXDOC(1)
  1655.  
  1656.  
  1657.               Note,  however, that unless you generate your scan-
  1658.               ner using the --CCff or --CCFF table compression options,
  1659.               use  of  --77  will save only a small amount of table
  1660.               space, and  make  your  scanner  considerably  less
  1661.               portable.   _F_l_e_x_'_s  default behavior is to generate
  1662.               an 8-bit scanner unless you use the --CCff or --CCFF,,  in
  1663.               which  case _f_l_e_x defaults to generating 7-bit scan-
  1664.               ners unless your site was always configured to gen-
  1665.               erate  8-bit  scanners  (as  will often be the case
  1666.               with non-USA sites).  You  can  tell  whether  flex
  1667.               generated a 7-bit or an 8-bit scanner by inspecting
  1668.               the flag summary in  the  --vv  output  as  described
  1669.               above.
  1670.  
  1671.               Note that if you use --CCffee or --CCFFee (those table com-
  1672.               pression  options,  but  also   using   equivalence
  1673.               classes   as   discussed  see  below),  flex  still
  1674.               defaults to generating an 8-bit scanner, since usu-
  1675.               ally  with  these  compression  options  full 8-bit
  1676.               tables are  not  much  more  expensive  than  7-bit
  1677.               tables.
  1678.  
  1679.        --88     instructs  _f_l_e_x to generate an 8-bit scanner, i.e.,
  1680.               one which can  recognize  8-bit  characters.   This
  1681.               flag  is  only  needed for scanners generated using
  1682.               --CCff or --CCFF,, as otherwise flex defaults to  generat-
  1683.               ing an 8-bit scanner anyway.
  1684.  
  1685.               See  the  discussion of --77 above for flex's default
  1686.               behavior and the tradeoffs between 7-bit and  8-bit
  1687.               scanners.
  1688.  
  1689.        --++     specifies  that  you  want  flex  to generate a C++
  1690.               scanner class.  See the section on  Generating  C++
  1691.               Scanners below for details.
  1692.  
  1693.        --CC[[aaeeffFFmmrr]]
  1694.               controls  the degree of table compression and, more
  1695.               generally, trade-offs between  small  scanners  and
  1696.               fast scanners.
  1697.  
  1698.               --CCaa  ("align")  instructs  flex to trade off larger
  1699.               tables in the generated scanner for faster  perfor-
  1700.               mance because the elements of the tables are better
  1701.               aligned for memory access and computation.  On some
  1702.               RISC architectures, fetching and manipulating long-
  1703.               words is more  efficient  than  with  smaller-sized
  1704.               datums  such as shortwords.  This option can double
  1705.               the size of the tables used by your scanner.
  1706.  
  1707.               --CCee directs _f_l_e_x to construct _e_q_u_i_v_a_l_e_n_c_e  _c_l_a_s_s_e_s_,
  1708.               i.e., sets of characters which have identical lexi-
  1709.               cal properties (for example, if the only appearance
  1710.               of  digits  in  the  _f_l_e_x input is in the character
  1711.  
  1712.  
  1713.  
  1714. Version 2.4               November 1993                        26
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720. FLEXDOC(1)                                             FLEXDOC(1)
  1721.  
  1722.  
  1723.               class "[0-9]" then the digits '0',  '1',  ...,  '9'
  1724.               will  all  be  put  in the same equivalence class).
  1725.               Equivalence classes usually  give  dramatic  reduc-
  1726.               tions  in  the final table/object file sizes (typi-
  1727.               cally a factor of 2-5) and are pretty cheap perfor-
  1728.               mance-wise   (one   array   look-up  per  character
  1729.               scanned).
  1730.  
  1731.               --CCff specifies that the _f_u_l_l scanner  tables  should
  1732.               be  generated - _f_l_e_x should not compress the tables
  1733.               by taking advantages of  similar  transition  func-
  1734.               tions for different states.
  1735.  
  1736.               --CCFF  specifies that the alternate fast scanner rep-
  1737.               resentation (described above  under  the  --FF  flag)
  1738.               should  be  used.   This option cannot be used with
  1739.               --++..
  1740.  
  1741.               --CCmm  directs  _f_l_e_x  to  construct  _m_e_t_a_-_e_q_u_i_v_a_l_e_n_c_e
  1742.               _c_l_a_s_s_e_s_,  which are sets of equivalence classes (or
  1743.               characters, if equivalence classes  are  not  being
  1744.               used)  that  are  commonly  used  together.   Meta-
  1745.               equivalence classes are often a big win when  using
  1746.               compressed tables, but they have a moderate perfor-
  1747.               mance impact (one or two "if" tests and  one  array
  1748.               look-up per character scanned).
  1749.  
  1750.               --CCrr  causes  the generated scanner to _b_y_p_a_s_s use of
  1751.               the  standard  I/O  library  (stdio)   for   input.
  1752.               Instead  of  calling ffrreeaadd(()) or ggeettcc(()),, the scanner
  1753.               will use the rreeaadd(()) system  call,  resulting  in  a
  1754.               performance  gain  which varies from system to sys-
  1755.               tem, but in general is probably  negligible  unless
  1756.               you are also using --CCff or --CCFF..  Using --CCrr can cause
  1757.               strange behavior if, for  example,  you  read  from
  1758.               _y_y_i_n  using  stdio  prior  to  calling  the scanner
  1759.               (because the scanner will miss whatever  text  your
  1760.               previous reads left in the stdio input buffer).
  1761.  
  1762.               --CCrr  has  no effect if you define YYYY__IINNPPUUTT (see The
  1763.               Generated Scanner above).
  1764.  
  1765.               A lone --CC specifies that the scanner tables  should
  1766.               be  compressed  but neither equivalence classes nor
  1767.               meta-equivalence classes should be used.
  1768.  
  1769.               The options --CCff or --CCFF and --CCmm do  not  make  sense
  1770.               together  -  there  is  no  opportunity  for  meta-
  1771.               equivalence classes if the table is not being  com-
  1772.               pressed.   Otherwise  the  options  may  be  freely
  1773.               mixed, and are cumulative.
  1774.  
  1775.               The default setting is --CCeemm,, which  specifies  that
  1776.               _f_l_e_x  should generate equivalence classes and meta-
  1777.  
  1778.  
  1779.  
  1780. Version 2.4               November 1993                        27
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786. FLEXDOC(1)                                             FLEXDOC(1)
  1787.  
  1788.  
  1789.               equivalence classes.   This  setting  provides  the
  1790.               highest degree of table compression.  You can trade
  1791.               off faster-executing scanners at the cost of larger
  1792.               tables with the following generally being true:
  1793.  
  1794.                   slowest & smallest
  1795.                         -Cem
  1796.                         -Cm
  1797.                         -Ce
  1798.                         -C
  1799.                         -C{f,F}e
  1800.                         -C{f,F}
  1801.                         -C{f,F}a
  1802.                   fastest & largest
  1803.  
  1804.               Note  that  scanners  with  the smallest tables are
  1805.               usually generated and  compiled  the  quickest,  so
  1806.               during development you will usually want to use the
  1807.               default, maximal compression.
  1808.  
  1809.               --CCffee is often a good compromise between  speed  and
  1810.               size for production scanners.
  1811.  
  1812.        --PPpprreeffiixx
  1813.               changes  the default _y_y prefix used by _f_l_e_x for all
  1814.               globally-visible variable  and  function  names  to
  1815.               instead  be _p_r_e_f_i_x_.  For example, --PPffoooo changes the
  1816.               name of yyyytteexxtt to ffooootteexxtt..   It  also  changes  the
  1817.               name  of  the  default output file from lleexx..yyyy..cc to
  1818.               lleexx..ffoooo..cc..  Here are all of the names affected:
  1819.  
  1820.                   yyFlexLexer
  1821.                   yy_create_buffer
  1822.                   yy_delete_buffer
  1823.                   yy_flex_debug
  1824.                   yy_init_buffer
  1825.                   yy_load_buffer_state
  1826.                   yy_switch_to_buffer
  1827.                   yyin
  1828.                   yyleng
  1829.                   yylex
  1830.                   yyout
  1831.                   yyrestart
  1832.                   yytext
  1833.                   yywrap
  1834.  
  1835.               Within your scanner itself, you can still refer  to
  1836.               the  global  variables  and  functions using either
  1837.               version of their name; but eternally, they have the
  1838.               modified name.
  1839.  
  1840.               This  option lets you easily link together multiple
  1841.               _f_l_e_x programs  into  the  same  executable.   Note,
  1842.               though,   that   using  this  option  also  renames
  1843.  
  1844.  
  1845.  
  1846. Version 2.4               November 1993                        28
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852. FLEXDOC(1)                                             FLEXDOC(1)
  1853.  
  1854.  
  1855.               yyyywwrraapp(()),, so you now _m_u_s_t provide your own  (appro-
  1856.               priately-named)  version  of  the  routine for your
  1857.               scanner, as linking with --llffll  no  longer  provides
  1858.               one for you by default.
  1859.  
  1860.        --SSsskkeelleettoonn__ffiillee
  1861.               overrides the default skeleton file from which _f_l_e_x
  1862.               constructs its scanners.  You'll  never  need  this
  1863.               option  unless  you  are  doing _f_l_e_x maintenance or
  1864.               development.
  1865.  
  1866. PPEERRFFOORRMMAANNCCEE CCOONNSSIIDDEERRAATTIIOONNSS
  1867.        The main design goal of _f_l_e_x is  that  it  generate  high-
  1868.        performance  scanners.   It has been optimized for dealing
  1869.        well with large sets of rules.  Aside from the effects  on
  1870.        scanner speed of the table compression --CC options outlined
  1871.        above, there are a number of options/actions which degrade
  1872.        performance.  These are, from most expensive to least:
  1873.  
  1874.            REJECT
  1875.  
  1876.            pattern sets that require backing up
  1877.            arbitrary trailing context
  1878.  
  1879.            yymore()
  1880.            '^' beginning-of-line operator
  1881.  
  1882.        with  the  first  three  all being quite expensive and the
  1883.        last two being quite cheap.  Note  also  that  uunnppuutt(())  is
  1884.        implemented  as a routine call that potentially does quite
  1885.        a bit of work, while yyyylleessss(()) is a quite-cheap  macro;  so
  1886.        if  just  putting  back  some excess text you scanned, use
  1887.        yyyylleessss(())..
  1888.  
  1889.        RREEJJEECCTT should be avoided at all costs when performance  is
  1890.        important.  It is a particularly expensive option.
  1891.  
  1892.        Getting  rid  of  backing  up is messy and often may be an
  1893.        enormous amount of work for  a  complicated  scanner.   In
  1894.        principal,  one  begins by using the --bb flag to generate a
  1895.        _l_e_x_._b_a_c_k_u_p file.  For example, on the input
  1896.  
  1897.            %%
  1898.            foo        return TOK_KEYWORD;
  1899.            foobar     return TOK_KEYWORD;
  1900.  
  1901.        the file looks like:
  1902.  
  1903.            State #6 is non-accepting -
  1904.             associated rule line numbers:
  1905.                   2       3
  1906.             out-transitions: [ o ]
  1907.             jam-transitions: EOF [ \001-n  p-\177 ]
  1908.  
  1909.  
  1910.  
  1911.  
  1912. Version 2.4               November 1993                        29
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918. FLEXDOC(1)                                             FLEXDOC(1)
  1919.  
  1920.  
  1921.            State #8 is non-accepting -
  1922.             associated rule line numbers:
  1923.                   3
  1924.             out-transitions: [ a ]
  1925.             jam-transitions: EOF [ \001-`  b-\177 ]
  1926.  
  1927.            State #9 is non-accepting -
  1928.             associated rule line numbers:
  1929.                   3
  1930.             out-transitions: [ r ]
  1931.             jam-transitions: EOF [ \001-q  s-\177 ]
  1932.  
  1933.            Compressed tables always back up.
  1934.  
  1935.        The first few lines tell us that there's a  scanner  state
  1936.        in which it can make a transition on an 'o' but not on any
  1937.        other character, and that  in  that  state  the  currently
  1938.        scanned  text  does  not match any rule.  The state occurs
  1939.        when trying to match the rules found at lines 2 and  3  in
  1940.        the  input file.  If the scanner is in that state and then
  1941.        reads something other than an 'o', it will have to back up
  1942.        to  find  a  rule  which  is matched.  With a bit of head-
  1943.        scratching one can see that this must be the state it's in
  1944.        when  it  has  seen "fo".  When this has happened, if any-
  1945.        thing other than another 'o' is  seen,  the  scanner  will
  1946.        have  to  back  up to simply match the 'f' (by the default
  1947.        rule).
  1948.  
  1949.        The comment regarding State #8 indicates there's a problem
  1950.        when  "foob"  has  been scanned.  Indeed, on any character
  1951.        other than an 'a', the scanner will have  to  back  up  to
  1952.        accept  "foo".   Similarly,  the comment for State #9 con-
  1953.        cerns when "fooba" has been scanned and an  'r'  does  not
  1954.        follow.
  1955.  
  1956.        The  final  comment reminds us that there's no point going
  1957.        to all the trouble of removing backing up from  the  rules
  1958.        unless  we're  using  --CCff or --CCFF,, since there's no perfor-
  1959.        mance gain doing so with compressed scanners.
  1960.  
  1961.        The way to remove the backing up is to add "error" rules:
  1962.  
  1963.            %%
  1964.            foo         return TOK_KEYWORD;
  1965.            foobar      return TOK_KEYWORD;
  1966.  
  1967.            fooba       |
  1968.            foob        |
  1969.            fo          {
  1970.                        /* false alarm, not really a keyword */
  1971.                        return TOK_ID;
  1972.                        }
  1973.  
  1974.  
  1975.  
  1976.  
  1977.  
  1978. Version 2.4               November 1993                        30
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984. FLEXDOC(1)                                             FLEXDOC(1)
  1985.  
  1986.  
  1987.        Eliminating backing up among a list of keywords  can  also
  1988.        be done using a "catch-all" rule:
  1989.  
  1990.            %%
  1991.            foo         return TOK_KEYWORD;
  1992.            foobar      return TOK_KEYWORD;
  1993.  
  1994.            [a-z]+      return TOK_ID;
  1995.  
  1996.        This is usually the best solution when appropriate.
  1997.  
  1998.        Backing  up  messages tend to cascade.  With a complicated
  1999.        set of rules it's not uncommon to  get  hundreds  of  mes-
  2000.        sages.   If  one  can decipher them, though, it often only
  2001.        takes a dozen or so rules  to  eliminate  the  backing  up
  2002.        (though it's easy to make a mistake and have an error rule
  2003.        accidentally match a valid token.  A possible future  _f_l_e_x
  2004.        feature  will  be  to automatically add rules to eliminate
  2005.        backing up).
  2006.  
  2007.        _V_a_r_i_a_b_l_e trailing context  (where  both  the  leading  and
  2008.        trailing  parts do not have a fixed length) entails almost
  2009.        the same performance loss as RREEJJEECCTT  (i.e.,  substantial).
  2010.        So when possible a rule like:
  2011.  
  2012.            %%
  2013.            mouse|rat/(cat|dog)   run();
  2014.  
  2015.        is better written:
  2016.  
  2017.            %%
  2018.            mouse/cat|dog         run();
  2019.            rat/cat|dog           run();
  2020.  
  2021.        or as
  2022.  
  2023.            %%
  2024.            mouse|rat/cat         run();
  2025.            mouse|rat/dog         run();
  2026.  
  2027.        Note that here the special '|' action does _n_o_t provide any
  2028.        savings, and can even make things worse (see
  2029.  
  2030.        A final note regarding performance: as mentioned above  in
  2031.        the section How the Input is Matched, dynamically resizing
  2032.        yyyytteexxtt to accomodate huge tokens is a slow process because
  2033.        it  presently  requires that the (huge) token be rescanned
  2034.        from the beginning.  Thus if  performance  is  vital,  you
  2035.        should attempt to match "large" quantities of text but not
  2036.        "huge" quantities, where the cutoff between the two is  at
  2037.        about 8K characters/token.
  2038.  
  2039.        Another  area where the user can increase a scanner's per-
  2040.        formance (and one that's easier to implement) arises  from
  2041.  
  2042.  
  2043.  
  2044. Version 2.4               November 1993                        31
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050. FLEXDOC(1)                                             FLEXDOC(1)
  2051.  
  2052.  
  2053.        the  fact  that  the longer the tokens matched, the faster
  2054.        the scanner will run.  This is because  with  long  tokens
  2055.        the processing of most input characters takes place in the
  2056.        (short) inner scanning loop, and does not often have to go
  2057.        through  the  additional  work  of setting up the scanning
  2058.        environment (e.g., yyyytteexxtt)) for  the  action.   Recall  the
  2059.        scanner for C comments:
  2060.  
  2061.            %x comment
  2062.            %%
  2063.                    int line_num = 1;
  2064.  
  2065.            "/*"         BEGIN(comment);
  2066.  
  2067.            <comment>[^*\n]*
  2068.            <comment>"*"+[^*/\n]*
  2069.            <comment>\n             ++line_num;
  2070.            <comment>"*"+"/"        BEGIN(INITIAL);
  2071.  
  2072.        This could be sped up by writing it as:
  2073.  
  2074.            %x comment
  2075.            %%
  2076.                    int line_num = 1;
  2077.  
  2078.            "/*"         BEGIN(comment);
  2079.  
  2080.            <comment>[^*\n]*
  2081.            <comment>[^*\n]*\n      ++line_num;
  2082.            <comment>"*"+[^*/\n]*
  2083.            <comment>"*"+[^*/\n]*\n ++line_num;
  2084.            <comment>"*"+"/"        BEGIN(INITIAL);
  2085.  
  2086.        Now  instead  of  each newline requiring the processing of
  2087.        another action, recognizing the newlines is  "distributed"
  2088.        over  the  other rules to keep the matched text as long as
  2089.        possible.  Note that _a_d_d_i_n_g rules does _n_o_t slow  down  the
  2090.        scanner!   The  speed of the scanner is independent of the
  2091.        number of rules or (modulo the considerations given at the
  2092.        beginning  of  this section) how complicated the rules are
  2093.        with regard to operators such as '*' and '|'.
  2094.  
  2095.        A final example in speeding up a scanner: suppose you want
  2096.        to  scan  through  a  file containing identifiers and key-
  2097.        words, one per line and with no other  extraneous  charac-
  2098.        ters,  and  recognize  all  the keywords.  A natural first
  2099.        approach is:
  2100.  
  2101.            %%
  2102.            asm      |
  2103.            auto     |
  2104.            break    |
  2105.            ... etc ...
  2106.            volatile |
  2107.  
  2108.  
  2109.  
  2110. Version 2.4               November 1993                        32
  2111.  
  2112.  
  2113.  
  2114.  
  2115.  
  2116. FLEXDOC(1)                                             FLEXDOC(1)
  2117.  
  2118.  
  2119.            while    /* it's a keyword */
  2120.  
  2121.            .|\n     /* it's not a keyword */
  2122.  
  2123.        To eliminate  the  back-tracking,  introduce  a  catch-all
  2124.        rule:
  2125.  
  2126.            %%
  2127.            asm      |
  2128.            auto     |
  2129.            break    |
  2130.            ... etc ...
  2131.            volatile |
  2132.            while    /* it's a keyword */
  2133.  
  2134.            [a-z]+   |
  2135.            .|\n     /* it's not a keyword */
  2136.  
  2137.        Now,  if it's guaranteed that there's exactly one word per
  2138.        line, then we can reduce the total number of matches by  a
  2139.        half  by  merging in the recognition of newlines with that
  2140.        of the other tokens:
  2141.  
  2142.            %%
  2143.            asm\n    |
  2144.            auto\n   |
  2145.            break\n  |
  2146.            ... etc ...
  2147.            volatile\n |
  2148.            while\n  /* it's a keyword */
  2149.  
  2150.            [a-z]+\n |
  2151.            .|\n     /* it's not a keyword */
  2152.  
  2153.        One has to be careful here, as we  have  now  reintroduced
  2154.        backing up into the scanner.  In particular, while _w_e know
  2155.        that there will never  be  any  characters  in  the  input
  2156.        stream  other  than letters or newlines, _f_l_e_x can't figure
  2157.        this out, and it will plan for possibly needing to back up
  2158.        when  it has scanned a token like "auto" and then the next
  2159.        character is something other than a newline or  a  letter.
  2160.        Previously it would then just match the "auto" rule and be
  2161.        done, but now it has no "auto" rule, only a "auto\n" rule.
  2162.        To  eliminate  the  possibility  of  backing  up, we could
  2163.        either duplicate all rules but without final newlines, or,
  2164.        since  we  never  expect  to  encounter  such an input and
  2165.        therefore don't how it's classified, we can introduce  one
  2166.        more catch-all rule, this one which doesn't include a new-
  2167.        line:
  2168.  
  2169.            %%
  2170.            asm\n    |
  2171.            auto\n   |
  2172.            break\n  |
  2173.  
  2174.  
  2175.  
  2176. Version 2.4               November 1993                        33
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182. FLEXDOC(1)                                             FLEXDOC(1)
  2183.  
  2184.  
  2185.            ... etc ...
  2186.            volatile\n |
  2187.            while\n  /* it's a keyword */
  2188.  
  2189.            [a-z]+\n |
  2190.            [a-z]+   |
  2191.            .|\n     /* it's not a keyword */
  2192.  
  2193.        Compiled with --CCff,, this is about as fast as one can get  a
  2194.        _f_l_e_x scanner to go for this particular problem.
  2195.  
  2196.        A  final  note: _f_l_e_x is slow when matching NUL's, particu-
  2197.        larly when a token contains multiple NUL's.  It's best  to
  2198.        write  rules  which  match  _s_h_o_r_t  amounts of text if it's
  2199.        anticipated that the text will often include NUL's.
  2200.  
  2201. GGEENNEERRAATTIINNGG CC++++ SSCCAANNNNEERRSS
  2202.        _f_l_e_x provides two different ways to generate scanners  for
  2203.        use  with C++.  The first way is to simply compile a scan-
  2204.        ner generated by _f_l_e_x using a C++ compiler instead of a  C
  2205.        compiler.   You  should  not  encounter  any  compilations
  2206.        errors (please report any you find to  the  email  address
  2207.        given  in the Author section below).  You can then use C++
  2208.        code in your rule actions instead of C  code.   Note  that
  2209.        the  default  input  source for your scanner remains _y_y_i_n_,
  2210.        and default echoing is still done to _y_y_o_u_t_.  Both of these
  2211.        remain _F_I_L_E _* variables and not C++ _s_t_r_e_a_m_s_.
  2212.  
  2213.        You  can  also  use  _f_l_e_x to generate a C++ scanner class,
  2214.        using the --++ option, which is automatically  specified  if
  2215.        the  name  of  the  flex executable ends in a '+', such as
  2216.        _f_l_e_x_+_+_.  When using this option, flex defaults to generat-
  2217.        ing the scanner to the file lleexx..yyyy..cccc instead of lleexx..yyyy..cc..
  2218.        The   generated   scanner   includes   the   header   file
  2219.        _F_l_e_x_L_e_x_e_r_._h_,  which  defines  the  interface  to  two  C++
  2220.        classes.
  2221.  
  2222.        The first class,  FFlleexxLLeexxeerr,,  provides  an  abstract  base
  2223.        class  defining  the  general scanner class interface.  It
  2224.        provides the following member functions:
  2225.  
  2226.        ccoonnsstt cchhaarr** YYYYTTeexxtt(())
  2227.               returns the  text  of  the  most  recently  matched
  2228.               token, the equivalent of yyyytteexxtt..
  2229.  
  2230.        iinntt YYYYLLeenngg(())
  2231.               returns  the  length  of  the most recently matched
  2232.               token, the equivalent of yyyylleenngg..
  2233.  
  2234.        Also  provided  are   member   functions   equivalent   to
  2235.        yyyy__sswwiittcchh__ttoo__bbuuffffeerr(()),,   yyyy__ccrreeaattee__bbuuffffeerr(())   (though  the
  2236.        first argument is an iissttrreeaamm** object  pointer  and  not  a
  2237.        FFIILLEE**)),,  yyyy__ddeelleettee__bbuuffffeerr(()),,  and  yyyyrreessttaarrtt(()) (again, the
  2238.        first argument is a iissttrreeaamm** object pointer).
  2239.  
  2240.  
  2241.  
  2242. Version 2.4               November 1993                        34
  2243.  
  2244.  
  2245.  
  2246.  
  2247.  
  2248. FLEXDOC(1)                                             FLEXDOC(1)
  2249.  
  2250.  
  2251.        The second class defined in  _F_l_e_x_L_e_x_e_r_._h  is  yyyyFFlleexxLLeexxeerr,,
  2252.        which is derived from FFlleexxLLeexxeerr..  It defines the following
  2253.        additional member functions:
  2254.  
  2255.        yyyyFFlleexxLLeexxeerr(( iissttrreeaamm** aarrgg__yyyyiinn == 00,, oossttrreeaamm** aarrgg__yyyyoouutt == 00
  2256.               ))
  2257.               constructs  a  yyyyFFlleexxLLeexxeerr  object  using the given
  2258.               streams for input and output.   If  not  specified,
  2259.               the  streams default to cciinn and ccoouutt,, respectively.
  2260.  
  2261.        vviirrttuuaall iinntt yyyylleexx(())
  2262.               performs the same role is yyyylleexx(()) does for ordinary
  2263.               flex scanners: it scans the input stream, consuming
  2264.               tokens, until a rule's action returns a value.
  2265.  
  2266.        In addition, yyyyFFlleexxLLeexxeerr defines the  following  protected
  2267.        virtual  functions  which  you  can  redefine  in  derived
  2268.        classes to tailor the scanner:
  2269.  
  2270.        vviirrttuuaall iinntt LLeexxeerrIInnppuutt(( cchhaarr** bbuuff,, iinntt mmaaxx__ssiizzee ))
  2271.               reads  up  to  mmaaxx__ssiizzee  characters  into  bbuuff  and
  2272.               returns the number of characters read.  To indicate
  2273.               end-of-input,  return  0  characters.   Note   that
  2274.               "interactive"  scanners  (see  the --BB and --II flags)
  2275.               define the macro YYYY__IINNTTEERRAACCTTIIVVEE..  If  you  redefine
  2276.               LLeexxeerrIInnppuutt(())  and  need  to  take different actions
  2277.               depending on whether or not the  scanner  might  be
  2278.               scanning  an interactive input source, you can test
  2279.               for the presence of this name via ##iiffddeeff..
  2280.  
  2281.        vviirrttuuaall vvooiidd LLeexxeerrOOuuttppuutt(( ccoonnsstt cchhaarr** bbuuff,, iinntt ssiizzee ))
  2282.               writes out ssiizzee characters  from  the  buffer  bbuuff,,
  2283.               which,   while  NUL-terminated,  may  also  contain
  2284.               "internal" NUL's if the scanner's rules  can  match
  2285.               text with NUL's in them.
  2286.  
  2287.        vviirrttuuaall vvooiidd LLeexxeerrEErrrroorr(( ccoonnsstt cchhaarr** mmssgg ))
  2288.               reports a fatal error message.  The default version
  2289.               of this function writes the message to  the  stream
  2290.               cceerrrr and exits.
  2291.  
  2292.        Note  that  a yyyyFFlleexxLLeexxeerr object contains its _e_n_t_i_r_e scan-
  2293.        ning state.  Thus you can use such objects to create reen-
  2294.        trant scanners.  You can instantiate multiple instances of
  2295.        the same yyyyFFlleexxLLeexxeerr class, and you can also combine  mul-
  2296.        tiple  C++  scanner  classes  together in the same program
  2297.        using the --PP option discussed above.
  2298.  
  2299.        Finally, note that the %%aarrrraayy feature is not available  to
  2300.        C++  scanner classes; you must use %%ppooiinntteerr (the default).
  2301.  
  2302.        Here is an example of a simple C++ scanner:
  2303.  
  2304.                // An example of using the flex C++ scanner class.
  2305.  
  2306.  
  2307.  
  2308. Version 2.4               November 1993                        35
  2309.  
  2310.  
  2311.  
  2312.  
  2313.  
  2314. FLEXDOC(1)                                             FLEXDOC(1)
  2315.  
  2316.  
  2317.            %{
  2318.            int mylineno = 0;
  2319.            %}
  2320.  
  2321.            string  \"[^\n"]+\"
  2322.  
  2323.            ws      [ \t]+
  2324.  
  2325.            alpha   [A-Za-z]
  2326.            dig     [0-9]
  2327.            name    ({alpha}|{dig}|\$)({alpha}|{dig}|[_.\-/$])*
  2328.            num1    [-+]?{dig}+\.?([eE][-+]?{dig}+)?
  2329.            num2    [-+]?{dig}*\.{dig}+([eE][-+]?{dig}+)?
  2330.            number  {num1}|{num2}
  2331.  
  2332.            %%
  2333.  
  2334.            {ws}    /* skip blanks and tabs */
  2335.  
  2336.            "/*"    {
  2337.                    int c;
  2338.  
  2339.                    while((c = yyinput()) != 0)
  2340.                        {
  2341.                        if(c == '\n')
  2342.                            ++mylineno;
  2343.  
  2344.                        else if(c == '*')
  2345.                            {
  2346.                            if((c = yyinput()) == '/')
  2347.                                break;
  2348.                            else
  2349.                                unput(c);
  2350.                            }
  2351.                        }
  2352.                    }
  2353.  
  2354.            {number}  cout << "number " << YYText() << '\n';
  2355.  
  2356.            \n        mylineno++;
  2357.  
  2358.            {name}    cout << "name " << YYText() << '\n';
  2359.  
  2360.            {string}  cout << "string " << YYText() << '\n';
  2361.  
  2362.            %%
  2363.  
  2364.            int main( int /* argc */, char** /* argv */ )
  2365.                {
  2366.                FlexLexer* lexer = new yyFlexLexer;
  2367.                while(lexer->yylex() != 0)
  2368.                    ;
  2369.                return 0;
  2370.                }
  2371.  
  2372.  
  2373.  
  2374. Version 2.4               November 1993                        36
  2375.  
  2376.  
  2377.  
  2378.  
  2379.  
  2380. FLEXDOC(1)                                             FLEXDOC(1)
  2381.  
  2382.  
  2383.        IMPORTANT: the present  form  of  the  scanning  class  is
  2384.        _e_x_p_e_r_i_m_e_n_t_a_l  and  may  change  considerably between major
  2385.        releases.
  2386.  
  2387. IINNCCOOMMPPAATTIIBBIILLIITTIIEESS WWIITTHH LLEEXX AANNDD PPOOSSIIXX
  2388.        _f_l_e_x is a rewrite of the  AT&T  Unix  _l_e_x  tool  (the  two
  2389.        implementations  do not share any code, though), with some
  2390.        extensions and incompatibilities, both  of  which  are  of
  2391.        concern  to those who wish to write scanners acceptable to
  2392.        either implementation.  The  POSIX  _l_e_x  specification  is
  2393.        closer  to  _f_l_e_x_'_s  behavior than that of the original _l_e_x
  2394.        implementation, but there also remain some  incompatibili-
  2395.        ties  between  _f_l_e_x  and  POSIX.  The intent is that ulti-
  2396.        mately _f_l_e_x will be fully POSIX-conformant.  In this  sec-
  2397.        tion we discuss all of the known areas of incompatibility.
  2398.  
  2399.        _f_l_e_x_'_s --ll option turns on maximum compatibility  with  the
  2400.        original  AT&T  _l_e_x implementation, at the cost of a major
  2401.        loss in the  generated  scanner's  performance.   We  note
  2402.        below which incompatibilities can be overcome using the --ll
  2403.        option.
  2404.  
  2405.        _f_l_e_x is fully  compatible  with  _l_e_x  with  the  following
  2406.        exceptions:
  2407.  
  2408.        -      The  undocumented  _l_e_x  scanner  internal  variable
  2409.               yyyylliinneennoo is not supported unless --ll is used.
  2410.  
  2411.               yylineno is not part of the POSIX specification.
  2412.  
  2413.        -      The iinnppuutt(()) routine is not redefinable,  though  it
  2414.               may be called to read characters following whatever
  2415.               has been matched by a rule.  If iinnppuutt(())  encounters
  2416.               an  end-of-file  the  normal yyyywwrraapp(()) processing is
  2417.               done.   A  ``real''  end-of-file  is  returned   by
  2418.               iinnppuutt(()) as _E_O_F_.
  2419.  
  2420.               Input   is   instead  controlled  by  defining  the
  2421.               YYYY__IINNPPUUTT macro.
  2422.  
  2423.               The _f_l_e_x restriction that iinnppuutt(()) cannot  be  rede-
  2424.               fined  is  in  accordance with the POSIX specifica-
  2425.               tion, which simply does not specify any way of con-
  2426.               trolling  the  scanner's input other than by making
  2427.               an initial assignment to _y_y_i_n_.
  2428.  
  2429.        -      _f_l_e_x scanners are not as reentrant as _l_e_x scanners.
  2430.               In  particular,  if you have an interactive scanner
  2431.               and an interrupt handler which  long-jumps  out  of
  2432.               the scanner, and the scanner is subsequently called
  2433.               again, you may get the following message:
  2434.  
  2435.                   fatal flex scanner internal error--end of buffer missed
  2436.  
  2437.  
  2438.  
  2439.  
  2440. Version 2.4               November 1993                        37
  2441.  
  2442.  
  2443.  
  2444.  
  2445.  
  2446. FLEXDOC(1)                                             FLEXDOC(1)
  2447.  
  2448.  
  2449.               To reenter the scanner, first use
  2450.  
  2451.                   yyrestart( yyin );
  2452.  
  2453.               Note that this call will throw  away  any  buffered
  2454.               input;  usually this isn't a problem with an inter-
  2455.               active scanner.
  2456.  
  2457.               Also note that flex C++ scanner classes  _a_r_e  reen-
  2458.               trant,  so  if  using C++ is an option for you, you
  2459.               should use them instead.  See "Generating C++ Scan-
  2460.               ners" above for details.
  2461.  
  2462.        -      oouuttppuutt(())  is  not  supported.  Output from the EECCHHOO
  2463.               macro is done to the  file-pointer  _y_y_o_u_t  (default
  2464.               _s_t_d_o_u_t_)_.
  2465.  
  2466.               oouuttppuutt(()) is not part of the POSIX specification.
  2467.  
  2468.        -      _l_e_x  does  not  support  exclusive start conditions
  2469.               (%x), though they are in the POSIX specification.
  2470.  
  2471.        -      When definitions are expanded, _f_l_e_x  encloses  them
  2472.               in parentheses.  With lex, the following:
  2473.  
  2474.                   NAME    [A-Z][A-Z0-9]*
  2475.                   %%
  2476.                   foo{NAME}?      printf( "Found it\n" );
  2477.                   %%
  2478.  
  2479.               will  not  match  the string "foo" because when the
  2480.               macro is expanded the rule is equivalent to "foo[A-
  2481.               Z][A-Z0-9]*?"   and the precedence is such that the
  2482.               '?' is associated with "[A-Z0-9]*".  With _f_l_e_x_, the
  2483.               rule will be expanded to "foo([A-Z][A-Z0-9]*)?" and
  2484.               so the string "foo" will match.
  2485.  
  2486.               Note that if the definition begins with ^^  or  ends
  2487.               with $$ then it is _n_o_t expanded with parentheses, to
  2488.               allow these  operators  to  appear  in  definitions
  2489.               without  losing  their  special  meanings.  But the
  2490.               <<ss>>,, //,, and <<<<EEOOFF>>>> operators cannot be used  in  a
  2491.               _f_l_e_x definition.
  2492.  
  2493.               Using  --ll  results in the _l_e_x behavior of no paren-
  2494.               theses around the definition.
  2495.  
  2496.               The POSIX specification is that the  definition  be
  2497.               enclosed in parentheses.
  2498.  
  2499.        -      The  _l_e_x  %%rr  (generate a Ratfor scanner) option is
  2500.               not supported.  It is not part of the POSIX  speci-
  2501.               fication.
  2502.  
  2503.  
  2504.  
  2505.  
  2506. Version 2.4               November 1993                        38
  2507.  
  2508.  
  2509.  
  2510.  
  2511.  
  2512. FLEXDOC(1)                                             FLEXDOC(1)
  2513.  
  2514.  
  2515.        -      After  a  call  to  uunnppuutt(()),,  _y_y_t_e_x_t and _y_y_l_e_n_g are
  2516.               undefined until the next token is  matched,  unless
  2517.               the  scanner  was  built using %%aarrrraayy..  This is not
  2518.               the case with _l_e_x or the POSIX specification.   The
  2519.               --ll option does away with this incompatibility.
  2520.  
  2521.        -      The  precedence  of the {{}} (numeric range) operator
  2522.               is different.  _l_e_x interprets "abc{1,3}" as  "match
  2523.               one,  two,  or three occurrences of 'abc'", whereas
  2524.               _f_l_e_x interprets it as "match 'ab' followed by  one,
  2525.               two,  or  three occurrences of 'c'".  The latter is
  2526.               in agreement with the POSIX specification.
  2527.  
  2528.        -      The precedence of the ^^ operator is different.  _l_e_x
  2529.               interprets "^foo|bar" as "match either 'foo' at the
  2530.               beginning of a line, or  'bar'  anywhere",  whereas
  2531.               _f_l_e_x  interprets it as "match either 'foo' or 'bar'
  2532.               if they come at the beginning of a line".  The lat-
  2533.               ter is in agreement with the POSIX specification.
  2534.  
  2535.        -      _y_y_i_n  is  _i_n_i_t_i_a_l_i_z_e_d  by _l_e_x to be _s_t_d_i_n_; _f_l_e_x_, on
  2536.               the other hand, initializes _y_y_i_n to NULL  and  then
  2537.               _a_s_s_i_g_n_s  it  to _s_t_d_i_n the first time the scanner is
  2538.               called,  providing  _y_y_i_n  has  not   already   been
  2539.               assigned  to  a  non-NULL value.  The difference is
  2540.               subtle, but the net effect is that with _f_l_e_x  scan-
  2541.               ners,  _y_y_i_n  does  not have a valid value until the
  2542.               scanner has been called.
  2543.  
  2544.               The --ll option does away with this  incompatibility.
  2545.  
  2546.        -      The special table-size declarations such as %%aa sup-
  2547.               ported by _l_e_x are not required  by  _f_l_e_x  scanners;
  2548.               _f_l_e_x ignores them.
  2549.  
  2550.        -      The  name FLEX_SCANNER is #define'd so scanners may
  2551.               be written for use with either _f_l_e_x or _l_e_x_.
  2552.  
  2553.        The following _f_l_e_x features are not included in _l_e_x or the
  2554.        POSIX specification:
  2555.  
  2556.            yyterminate()
  2557.            <<EOF>>
  2558.            <*>
  2559.            YY_DECL
  2560.            YY_START
  2561.            YY_USER_ACTION
  2562.            #line directives
  2563.            %{}'s around actions
  2564.            multiple actions on a line
  2565.  
  2566.        plus  almost  all  of the flex flags.  The last feature in
  2567.        the list refers to the fact that with  _f_l_e_x  you  can  put
  2568.        multiple  actions  on  the same line, separated with semi-
  2569.  
  2570.  
  2571.  
  2572. Version 2.4               November 1993                        39
  2573.  
  2574.  
  2575.  
  2576.  
  2577.  
  2578. FLEXDOC(1)                                             FLEXDOC(1)
  2579.  
  2580.  
  2581.        colons, while with _l_e_x_, the following
  2582.  
  2583.            foo    handle_foo(); ++num_foos_seen;
  2584.  
  2585.        is (rather surprisingly) truncated to
  2586.  
  2587.            foo    handle_foo();
  2588.  
  2589.        _f_l_e_x does not truncate the action.  Actions that  are  not
  2590.        enclosed in braces are simply terminated at the end of the
  2591.        line.
  2592.  
  2593. DDIIAAGGNNOOSSTTIICCSS
  2594.        _w_a_r_n_i_n_g_, _r_u_l_e _c_a_n_n_o_t _b_e _m_a_t_c_h_e_d indicates that  the  given
  2595.        rule cannot be matched because it follows other rules that
  2596.        will always match the same text as it.   For  example,  in
  2597.        the  following  "foo"  cannot  be matched because it comes
  2598.        after an identifier "catch-all" rule:
  2599.  
  2600.            [a-z]+    got_identifier();
  2601.            foo       got_foo();
  2602.  
  2603.        Using RREEJJEECCTT in a scanner suppresses this warning.
  2604.  
  2605.        _w_a_r_n_i_n_g_, --ss _o_p_t_i_o_n _g_i_v_e_n _b_u_t _d_e_f_a_u_l_t _r_u_l_e _c_a_n  _b_e  _m_a_t_c_h_e_d
  2606.        means  that  it  is possible (perhaps only in a particular
  2607.        start condition) that the default rule (match  any  single
  2608.        character)  is  the  only one that will match a particular
  2609.        input.   Since  --ss  was  given,  presumably  this  is  not
  2610.        intended.
  2611.  
  2612.        _r_e_j_e_c_t___u_s_e_d___b_u_t___n_o_t___d_e_t_e_c_t_e_d          _u_n_d_e_f_i_n_e_d         or
  2613.        _y_y_m_o_r_e___u_s_e_d___b_u_t___n_o_t___d_e_t_e_c_t_e_d _u_n_d_e_f_i_n_e_d _- These errors  can
  2614.        occur  at  compile  time.   They indicate that the scanner
  2615.        uses RREEJJEECCTT or yyyymmoorree(()) but that _f_l_e_x failed to notice the
  2616.        fact,  meaning  that  _f_l_e_x  scanned the first two sections
  2617.        looking for occurrences of these  actions  and  failed  to
  2618.        find  any,  but  somehow you snuck some in (via a #include
  2619.        file, for example).  Make an  explicit  reference  to  the
  2620.        action  in  your  _f_l_e_x  input file.  (Note that previously
  2621.        _f_l_e_x supported a %%uusseedd//%%uunnuusseedd mechanism for dealing  with
  2622.        this problem; this feature is still supported but now dep-
  2623.        recated, and will go away soon  unless  the  author  hears
  2624.        from people who can argue compellingly that they need it.)
  2625.  
  2626.        _f_l_e_x _s_c_a_n_n_e_r _j_a_m_m_e_d _-  a  scanner  compiled  with  --ss  has
  2627.        encountered an input string which wasn't matched by any of
  2628.        its rules.  This error can  also  occur  due  to  internal
  2629.        problems.
  2630.  
  2631.        _t_o_k_e_n _t_o_o _l_a_r_g_e_, _e_x_c_e_e_d_s _Y_Y_L_M_A_X _- your scanner uses %%aarrrraayy
  2632.        and one of its rules matched a string longer than the YYYYLL--
  2633.        MMAAXX  constant (8K bytes by default).  You can increase the
  2634.        value by #define'ing YYYYLLMMAAXX in the definitions section  of
  2635.  
  2636.  
  2637.  
  2638. Version 2.4               November 1993                        40
  2639.  
  2640.  
  2641.  
  2642.  
  2643.  
  2644. FLEXDOC(1)                                             FLEXDOC(1)
  2645.  
  2646.  
  2647.        your _f_l_e_x input.
  2648.  
  2649.        _s_c_a_n_n_e_r  _r_e_q_u_i_r_e_s  _-_8 _f_l_a_g _t_o _u_s_e _t_h_e _c_h_a_r_a_c_t_e_r _'_x_' _- Your
  2650.        scanner specification includes recognizing the 8-bit char-
  2651.        acter  _'_x_'  and  you did not specify the -8 flag, and your
  2652.        scanner defaulted to 7-bit because you used the --CCff or --CCFF
  2653.        table  compression  options.  See the discussion of the --77
  2654.        flag for details.
  2655.  
  2656.        _f_l_e_x _s_c_a_n_n_e_r _p_u_s_h_-_b_a_c_k _o_v_e_r_f_l_o_w _- you used uunnppuutt(()) to push
  2657.        back so much text that the scanner's buffer could not hold
  2658.        both the pushed-back text and the current token in yyyytteexxtt..
  2659.        Ideally  the  scanner should dynamically resize the buffer
  2660.        in this case, but at present it does not.
  2661.  
  2662.        _i_n_p_u_t _b_u_f_f_e_r _o_v_e_r_f_l_o_w_, _c_a_n_'_t _e_n_l_a_r_g_e _b_u_f_f_e_r _b_e_c_a_u_s_e  _s_c_a_n_-
  2663.        _n_e_r  _u_s_e_s  _R_E_J_E_C_T _- the scanner was working on matching an
  2664.        extremely large token  and  needed  to  expand  the  input
  2665.        buffer.   This doesn't work with scanners that use RREEJJEECCTT..
  2666.  
  2667.        _f_a_t_a_l _f_l_e_x _s_c_a_n_n_e_r _i_n_t_e_r_n_a_l _e_r_r_o_r_-_-_e_n_d _o_f _b_u_f_f_e_r _m_i_s_s_e_d  _-
  2668.        This  can  occur  in an scanner which is reentered after a
  2669.        long-jump has jumped out (or over) the  scanner's  activa-
  2670.        tion frame.  Before reentering the scanner, use:
  2671.  
  2672.            yyrestart( yyin );
  2673.  
  2674.        or, as noted above, switch to using the C++ scanner class.
  2675.  
  2676.        _t_o_o _m_a_n_y _s_t_a_r_t _c_o_n_d_i_t_i_o_n_s _i_n _<_> _c_o_n_s_t_r_u_c_t_!  _-  you  listed
  2677.        more start conditions in a <> construct than exist (so you
  2678.        must have listed at least one of them twice).
  2679.  
  2680. FFIILLEESS
  2681.        See flex(1).
  2682.  
  2683. DDEEFFIICCIIEENNCCIIEESS // BBUUGGSS
  2684.        Again, see flex(1).
  2685.  
  2686. SSEEEE AALLSSOO
  2687.        flex(1), lex(1), yacc(1), sed(1), awk(1).
  2688.  
  2689.        M. E. Lesk and E. Schmidt, _L_E_X _- _L_e_x_i_c_a_l _A_n_a_l_y_z_e_r  _G_e_n_e_r_a_-
  2690.        _t_o_r
  2691.  
  2692. AAUUTTHHOORR
  2693.        Vern Paxson, with the help of many ideas and much inspira-
  2694.        tion  from  Van  Jacobson.   Original   version   by   Jef
  2695.        Poskanzer.   The  fast  table  representation is a partial
  2696.        implementation of a design  done  by  Van  Jacobson.   The
  2697.        implementation was done by Kevin Gong and Vern Paxson.
  2698.  
  2699.        Thanks  to  the  many  _f_l_e_x beta-testers, feedbackers, and
  2700.        contributors, especially Francois  Pinard,  Casey  Leedom,
  2701.  
  2702.  
  2703.  
  2704. Version 2.4               November 1993                        41
  2705.  
  2706.  
  2707.  
  2708.  
  2709.  
  2710. FLEXDOC(1)                                             FLEXDOC(1)
  2711.  
  2712.  
  2713.        Nelson  H.F.  Beebe, benson@odi.com, Peter A. Bigot, Keith
  2714.        Bostic, Frederic Brehm, Nick Christopher, Jason  Coughlin,
  2715.        Bill  Cox,  Dave  Curtis,  Scott  David  Daniels, Chris G.
  2716.        Demetriou, Mike Donahue, Chuck Doucette, Tom Epperly,  Leo
  2717.        Eskin,  Chris  Faylor,  Jon  Forrest, Kaveh R. Ghazi, Eric
  2718.        Goldman, Ulrich Grepel, Jan Hajic, Jarkko Hietaniemi, Eric
  2719.        Hughes,  John Interrante, Ceriel Jacobs, Jeffrey R. Jones,
  2720.        Henry Juengst,  Amir  Katz,  ken@ken.hilco.com,  Kevin  B.
  2721.        Kenny, Marq Kole, Ronald Lamprecht, Greg Lee, Craig Leres,
  2722.        John Levine, Steve Liddle, Mohamed el Lozy, Brian  Madsen,
  2723.        Chris  Metcalf,  Luke  Mewburn,  Jim Meyering, G.T. Nicol,
  2724.        Landon Noll, Marc Nozell,  Richard  Ohnemus,  Sven  Panne,
  2725.        Roland  Pesch,  Walter  Pelissero,  Gaumond Pierre, Esmond
  2726.        Pitt, Jef Poskanzer, Joe Rahmeh, Frederic Raimbault,  Rick
  2727.        Richardson,  Kevin  Rodgers,  Jim  Roskind,  Doug Schmidt,
  2728.        Philippe Schnoebelen, Andreas Schwab,  Alex  Siegel,  Mike
  2729.        Stump,  Paul  Stuart,  Dave  Tallman,  Chris Thewalt, Paul
  2730.        Tuinenga, Gary Weik, Frank Whaley, Gerhard Wilhelms,  Kent
  2731.        Williams,  Ken  Yap,  Nathan  Zelle, David Zuhn, and those
  2732.        whose names have slipped my marginal mail-archiving skills
  2733.        but whose contributions are appreciated all the same.
  2734.  
  2735.        Thanks  to  Keith Bostic, Jon Forrest, Noah Friedman, John
  2736.        Gilmore, Craig  Leres,  John  Levine,  Bob  Mulcahy,  G.T.
  2737.        Nicol,  Francois  Pinard,  Rich Salz, and Richard Stallman
  2738.        for help with various distribution headaches.
  2739.  
  2740.        Thanks to Esmond Pitt and Earle Horton for 8-bit character
  2741.        support;  to  Benson Margulies and Fred Burke for C++ sup-
  2742.        port; to Kent Williams and Tom Epperly for C++ class  sup-
  2743.        port;  to  Ove  Ewerlid  for support of NUL's; and to Eric
  2744.        Hughes for support of multiple buffers.
  2745.  
  2746.        This work was primarily done when I was with the Real Time
  2747.        Systems  Group  at  the  Lawrence  Berkeley  Laboratory in
  2748.        Berkeley, CA.  Many thanks to all there for the support  I
  2749.        received.
  2750.  
  2751.        Send comments to:
  2752.  
  2753.             Vern Paxson
  2754.             Systems Engineering
  2755.             Bldg. 46A, Room 1123
  2756.             Lawrence Berkeley Laboratory
  2757.             University of California
  2758.             Berkeley, CA 94720
  2759.  
  2760.             vern@ee.lbl.gov
  2761.  
  2762.  
  2763.  
  2764.  
  2765.  
  2766.  
  2767.  
  2768.  
  2769.  
  2770. Version 2.4               November 1993                        42
  2771.  
  2772.  
  2773.